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
7#include "unicode/utypes.h"
8#include "unicode/bytestream.h"
9#include "unicode/edits.h"
10#include "cmemory.h"
11#include "uassert.h"
12
13U_NAMESPACE_BEGIN
14
15class ByteSink;
16class CharString;
17class Edits;
18
19class U_COMMON_API ByteSinkUtil {
20public:
21 ByteSinkUtil() = delete; // all static
22
23 /** (length) bytes were mapped to valid (s16, s16Length). */
24 static UBool appendChange(int32_t length,
25 const char16_t *s16, int32_t s16Length,
26 ByteSink &sink, Edits *edits, UErrorCode &errorCode);
27
28 /** The bytes at [s, limit[ were mapped to valid (s16, s16Length). */
29 static UBool appendChange(const uint8_t *s, const uint8_t *limit,
30 const char16_t *s16, int32_t s16Length,
31 ByteSink &sink, Edits *edits, UErrorCode &errorCode);
32
33 /** (length) bytes were mapped/changed to valid code point c. */
34 static void appendCodePoint(int32_t length, UChar32 c, ByteSink &sink, Edits *edits = nullptr);
35
36 /** The few bytes at [src, nextSrc[ were mapped/changed to valid code point c. */
37 static inline void appendCodePoint(const uint8_t *src, const uint8_t *nextSrc, UChar32 c,
38 ByteSink &sink, Edits *edits = nullptr) {
39 appendCodePoint((int32_t)(nextSrc - src), c, sink, edits);
40 }
41
42 /** Append the two-byte character (U+0080..U+07FF). */
43 static void appendTwoBytes(UChar32 c, ByteSink &sink);
44
45 static UBool appendUnchanged(const uint8_t *s, int32_t length,
46 ByteSink &sink, uint32_t options, Edits *edits,
47 UErrorCode &errorCode) {
48 if (U_FAILURE(errorCode)) { return FALSE; }
49 if (length > 0) { appendNonEmptyUnchanged(s, length, sink, options, edits); }
50 return TRUE;
51 }
52
53 static UBool appendUnchanged(const uint8_t *s, const uint8_t *limit,
54 ByteSink &sink, uint32_t options, Edits *edits,
55 UErrorCode &errorCode);
56
57private:
58 static void appendNonEmptyUnchanged(const uint8_t *s, int32_t length,
59 ByteSink &sink, uint32_t options, Edits *edits);
60};
61
62class U_COMMON_API CharStringByteSink : public ByteSink {
63public:
64 CharStringByteSink(CharString* dest);
65 ~CharStringByteSink() override;
66
67 CharStringByteSink() = delete;
68 CharStringByteSink(const CharStringByteSink&) = delete;
69 CharStringByteSink& operator=(const CharStringByteSink&) = delete;
70
71 void Append(const char* bytes, int32_t n) override;
72
73 char* GetAppendBuffer(int32_t min_capacity,
74 int32_t desired_capacity_hint,
75 char* scratch,
76 int32_t scratch_capacity,
77 int32_t* result_capacity) override;
78
79private:
80 CharString& dest_;
81};
82
83U_NAMESPACE_END
84