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#ifndef BYTESINKUTIL_H
8#define BYTESINKUTIL_H
9
10#include "unicode/utypes.h"
11#include "unicode/bytestream.h"
12#include "unicode/edits.h"
13#include "cmemory.h"
14#include "uassert.h"
15
16U_NAMESPACE_BEGIN
17
18class ByteSink;
19class CharString;
20class Edits;
21
22class U_COMMON_API ByteSinkUtil {
23public:
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,
50 UErrorCode &errorCode) {
51 if (U_FAILURE(errorCode)) { return false; }
52 if (length > 0) { appendNonEmptyUnchanged(s, length, sink, options, edits); }
53 return true;
54 }
55
56 static UBool appendUnchanged(const uint8_t *s, const uint8_t *limit,
57 ByteSink &sink, uint32_t options, Edits *edits,
58 UErrorCode &errorCode);
59
60private:
61 static void appendNonEmptyUnchanged(const uint8_t *s, int32_t length,
62 ByteSink &sink, uint32_t options, Edits *edits);
63};
64
65class U_COMMON_API CharStringByteSink : public ByteSink {
66public:
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
82private:
83 CharString& dest_;
84};
85
86U_NAMESPACE_END
87
88#endif //BYTESINKUTIL_H
89