bytesinkutil.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // bytesinkutil.h
  4. // created: 2017sep14 Markus W. Scherer
  5. #include "unicode/utypes.h"
  6. #include "unicode/bytestream.h"
  7. #include "unicode/edits.h"
  8. #include "cmemory.h"
  9. #include "uassert.h"
  10. U_NAMESPACE_BEGIN
  11. class ByteSink;
  12. class CharString;
  13. class Edits;
  14. class U_COMMON_API ByteSinkUtil {
  15. public:
  16. ByteSinkUtil() = delete; // all static
  17. /** (length) bytes were mapped to valid (s16, s16Length). */
  18. static UBool appendChange(int32_t length,
  19. const char16_t *s16, int32_t s16Length,
  20. ByteSink &sink, Edits *edits, UErrorCode &errorCode);
  21. /** The bytes at [s, limit[ were mapped to valid (s16, s16Length). */
  22. static UBool appendChange(const uint8_t *s, const uint8_t *limit,
  23. const char16_t *s16, int32_t s16Length,
  24. ByteSink &sink, Edits *edits, UErrorCode &errorCode);
  25. /** (length) bytes were mapped/changed to valid code point c. */
  26. static void appendCodePoint(int32_t length, UChar32 c, ByteSink &sink, Edits *edits = nullptr);
  27. /** The few bytes at [src, nextSrc[ were mapped/changed to valid code point c. */
  28. static inline void appendCodePoint(const uint8_t *src, const uint8_t *nextSrc, UChar32 c,
  29. ByteSink &sink, Edits *edits = nullptr) {
  30. appendCodePoint((int32_t)(nextSrc - src), c, sink, edits);
  31. }
  32. /** Append the two-byte character (U+0080..U+07FF). */
  33. static void appendTwoBytes(UChar32 c, ByteSink &sink);
  34. static UBool appendUnchanged(const uint8_t *s, int32_t length,
  35. ByteSink &sink, uint32_t options, Edits *edits,
  36. UErrorCode &errorCode) {
  37. if (U_FAILURE(errorCode)) { return FALSE; }
  38. if (length > 0) { appendNonEmptyUnchanged(s, length, sink, options, edits); }
  39. return TRUE;
  40. }
  41. static UBool appendUnchanged(const uint8_t *s, const uint8_t *limit,
  42. ByteSink &sink, uint32_t options, Edits *edits,
  43. UErrorCode &errorCode);
  44. private:
  45. static void appendNonEmptyUnchanged(const uint8_t *s, int32_t length,
  46. ByteSink &sink, uint32_t options, Edits *edits);
  47. };
  48. class U_COMMON_API CharStringByteSink : public ByteSink {
  49. public:
  50. CharStringByteSink(CharString* dest);
  51. ~CharStringByteSink() override;
  52. CharStringByteSink() = delete;
  53. CharStringByteSink(const CharStringByteSink&) = delete;
  54. CharStringByteSink& operator=(const CharStringByteSink&) = delete;
  55. void Append(const char* bytes, int32_t n) override;
  56. char* GetAppendBuffer(int32_t min_capacity,
  57. int32_t desired_capacity_hint,
  58. char* scratch,
  59. int32_t scratch_capacity,
  60. int32_t* result_capacity) override;
  61. private:
  62. CharString& dest_;
  63. };
  64. U_NAMESPACE_END