| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef BYTESINKUTIL_H |
| 8 |
#define BYTESINKUTIL_H |
| 9 |
|
| 10 |
#include <type_traits> |
| 11 |
|
| 12 |
#include "unicode/utypes.h" |
| 13 |
#include "unicode/bytestream.h" |
| 14 |
#include "unicode/edits.h" |
| 15 |
#include "charstr.h" |
| 16 |
#include "cmemory.h" |
| 17 |
#include "uassert.h" |
| 18 |
#include "ustr_imp.h" |
| 19 |
|
| 20 |
U_NAMESPACE_BEGIN |
| 21 |
|
| 22 |
class ByteSink; |
| 23 |
class Edits; |
| 24 |
|
| 25 |
class U_COMMON_API CharStringByteSink : public ByteSink { |
| 26 |
public: |
| 27 |
CharStringByteSink(CharString* dest); |
| 28 |
~CharStringByteSink() override; |
| 29 |
|
| 30 |
CharStringByteSink() = delete; |
| 31 |
CharStringByteSink(const CharStringByteSink&) = delete; |
| 32 |
CharStringByteSink& operator=(const CharStringByteSink&) = delete; |
| 33 |
|
| 34 |
void Append(const char* bytes, int32_t n) override; |
| 35 |
|
| 36 |
char* GetAppendBuffer(int32_t min_capacity, |
| 37 |
int32_t desired_capacity_hint, |
| 38 |
char* scratch, |
| 39 |
int32_t scratch_capacity, |
| 40 |
int32_t* result_capacity) override; |
| 41 |
|
| 42 |
private: |
| 43 |
CharString& dest_; |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
template<> |
| 50 |
class StringByteSink<CharString> : public CharStringByteSink { |
| 51 |
public: |
| 52 |
StringByteSink(CharString* dest) : CharStringByteSink(dest) { } |
| 53 |
StringByteSink(CharString* dest, int32_t ) : CharStringByteSink(dest) { } |
| 54 |
}; |
| 55 |
|
| 56 |
class U_COMMON_API ByteSinkUtil { |
| 57 |
public: |
| 58 |
ByteSinkUtil() = delete; |
| 59 |
|
| 60 |
|
| 61 |
static UBool appendChange(int32_t length, |
| 62 |
const char16_t *s16, int32_t s16Length, |
| 63 |
ByteSink &sink, Edits *edits, UErrorCode &errorCode); |
| 64 |
|
| 65 |
|
| 66 |
static UBool appendChange(const uint8_t *s, const uint8_t *limit, |
| 67 |
const char16_t *s16, int32_t s16Length, |
| 68 |
ByteSink &sink, Edits *edits, UErrorCode &errorCode); |
| 69 |
|
| 70 |
|
| 71 |
static void appendCodePoint(int32_t length, UChar32 c, ByteSink &sink, Edits *edits = nullptr); |
| 72 |
|
| 73 |
|
| 74 |
static inline void appendCodePoint(const uint8_t *src, const uint8_t *nextSrc, UChar32 c, |
| 75 |
ByteSink &sink, Edits *edits = nullptr) { |
| 76 |
appendCodePoint(static_cast<int32_t>(nextSrc - src), c, sink, edits); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
static void appendTwoBytes(UChar32 c, ByteSink &sink); |
| 81 |
|
| 82 |
static UBool appendUnchanged(const uint8_t *s, int32_t length, |
| 83 |
ByteSink &sink, uint32_t options, Edits *edits, |
| 84 |
UErrorCode &errorCode) { |
| 85 |
if (U_FAILURE(errorCode)) { return false; } |
| 86 |
if (length > 0) { appendNonEmptyUnchanged(s, length, sink, options, edits); } |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
static UBool appendUnchanged(const uint8_t *s, const uint8_t *limit, |
| 91 |
ByteSink &sink, uint32_t options, Edits *edits, |
| 92 |
UErrorCode &errorCode); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
template <typename F, |
| 108 |
typename = std::enable_if_t< |
| 109 |
std::is_invocable_r_v<void, F, ByteSink&, UErrorCode&>>> |
| 110 |
static int32_t viaByteSinkToTerminatedChars(char* buffer, int32_t capacity, |
| 111 |
F&& lambda, |
| 112 |
UErrorCode& status) { |
| 113 |
if (U_FAILURE(status)) { return 0; } |
| 114 |
CheckedArrayByteSink sink(buffer, capacity); |
| 115 |
lambda(sink, status); |
| 116 |
if (U_FAILURE(status)) { return 0; } |
| 117 |
|
| 118 |
int32_t reslen = sink.NumberOfBytesAppended(); |
| 119 |
|
| 120 |
if (sink.Overflowed()) { |
| 121 |
status = U_BUFFER_OVERFLOW_ERROR; |
| 122 |
return reslen; |
| 123 |
} |
| 124 |
|
| 125 |
return u_terminateChars(buffer, capacity, reslen, &status); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
template <typename F, |
| 139 |
typename = std::enable_if_t< |
| 140 |
std::is_invocable_r_v<void, F, ByteSink&, UErrorCode&>>> |
| 141 |
static CharString viaByteSinkToCharString(F&& lambda, UErrorCode& status) { |
| 142 |
if (U_FAILURE(status)) { return {}; } |
| 143 |
CharString result; |
| 144 |
CharStringByteSink sink(&result); |
| 145 |
lambda(sink, status); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
private: |
| 150 |
static void appendNonEmptyUnchanged(const uint8_t *s, int32_t length, |
| 151 |
ByteSink &sink, uint32_t options, Edits *edits); |
| 152 |
}; |
| 153 |
|
| 154 |
U_NAMESPACE_END |
| 155 |
|
| 156 |
#endif |