Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1 | // © 2017 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | |
| 4 | // bytesinkutil.h |
| 5 | // created: 2017sep14 Markus W. Scherer |
| 6 | |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 7 | #ifndef BYTESINKUTIL_H |
| 8 | #define BYTESINKUTIL_H |
| 9 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 10 | #include "unicode/utypes.h" |
| 11 | #include "unicode/bytestream.h" |
| 12 | #include "unicode/edits.h" |
| 13 | #include "cmemory.h" |
| 14 | #include "uassert.h" |
| 15 | |
| 16 | U_NAMESPACE_BEGIN |
| 17 | |
| 18 | class ByteSink; |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 19 | class CharString; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 20 | class Edits; |
| 21 | |
| 22 | class U_COMMON_API ByteSinkUtil { |
| 23 | public: |
| 24 | ByteSinkUtil() = delete; // all static |
| 25 | |
| 26 | /** (length) bytes were mapped to valid (s16, s16Length). */ |
| 27 | static UBool appendChange(int32_t length, |
| 28 | const char16_t *s16, int32_t s16Length, |
| 29 | ByteSink &sink, Edits *edits, UErrorCode &errorCode); |
| 30 | |
| 31 | /** The bytes at [s, limit[ were mapped to valid (s16, s16Length). */ |
| 32 | static UBool appendChange(const uint8_t *s, const uint8_t *limit, |
| 33 | const char16_t *s16, int32_t s16Length, |
| 34 | ByteSink &sink, Edits *edits, UErrorCode &errorCode); |
| 35 | |
| 36 | /** (length) bytes were mapped/changed to valid code point c. */ |
| 37 | static void appendCodePoint(int32_t length, UChar32 c, ByteSink &sink, Edits *edits = nullptr); |
| 38 | |
| 39 | /** The few bytes at [src, nextSrc[ were mapped/changed to valid code point c. */ |
| 40 | static inline void appendCodePoint(const uint8_t *src, const uint8_t *nextSrc, UChar32 c, |
| 41 | ByteSink &sink, Edits *edits = nullptr) { |
| 42 | appendCodePoint((int32_t)(nextSrc - src), c, sink, edits); |
| 43 | } |
| 44 | |
| 45 | /** Append the two-byte character (U+0080..U+07FF). */ |
| 46 | static void appendTwoBytes(UChar32 c, ByteSink &sink); |
| 47 | |
| 48 | static UBool appendUnchanged(const uint8_t *s, int32_t length, |
| 49 | ByteSink &sink, uint32_t options, Edits *edits, |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 50 | UErrorCode &errorCode) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 51 | if (U_FAILURE(errorCode)) { return false; } |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 52 | if (length > 0) { appendNonEmptyUnchanged(s, length, sink, options, edits); } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 53 | return true; |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 54 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 55 | |
| 56 | static UBool appendUnchanged(const uint8_t *s, const uint8_t *limit, |
| 57 | ByteSink &sink, uint32_t options, Edits *edits, |
| 58 | UErrorCode &errorCode); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | static void appendNonEmptyUnchanged(const uint8_t *s, int32_t length, |
| 62 | ByteSink &sink, uint32_t options, Edits *edits); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 65 | class U_COMMON_API CharStringByteSink : public ByteSink { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 66 | public: |
| 67 | CharStringByteSink(CharString* dest); |
| 68 | ~CharStringByteSink() override; |
| 69 | |
| 70 | CharStringByteSink() = delete; |
| 71 | CharStringByteSink(const CharStringByteSink&) = delete; |
| 72 | CharStringByteSink& operator=(const CharStringByteSink&) = delete; |
| 73 | |
| 74 | void Append(const char* bytes, int32_t n) override; |
| 75 | |
| 76 | char* GetAppendBuffer(int32_t min_capacity, |
| 77 | int32_t desired_capacity_hint, |
| 78 | char* scratch, |
| 79 | int32_t scratch_capacity, |
| 80 | int32_t* result_capacity) override; |
| 81 | |
| 82 | private: |
| 83 | CharString& dest_; |
| 84 | }; |
| 85 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 86 | U_NAMESPACE_END |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 87 | |
| 88 | #endif //BYTESINKUTIL_H |