bytesinkutil.h 3.1 KB

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