Path: blob/master/thirdparty/icu4c/common/bytesinkutil.h
9903 views
// © 2017 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html23// bytesinkutil.h4// created: 2017sep14 Markus W. Scherer56#ifndef BYTESINKUTIL_H7#define BYTESINKUTIL_H89#include <type_traits>1011#include "unicode/utypes.h"12#include "unicode/bytestream.h"13#include "unicode/edits.h"14#include "charstr.h"15#include "cmemory.h"16#include "uassert.h"17#include "ustr_imp.h"1819U_NAMESPACE_BEGIN2021class ByteSink;22class Edits;2324class U_COMMON_API CharStringByteSink : public ByteSink {25public:26CharStringByteSink(CharString* dest);27~CharStringByteSink() override;2829CharStringByteSink() = delete;30CharStringByteSink(const CharStringByteSink&) = delete;31CharStringByteSink& operator=(const CharStringByteSink&) = delete;3233void Append(const char* bytes, int32_t n) override;3435char* GetAppendBuffer(int32_t min_capacity,36int32_t desired_capacity_hint,37char* scratch,38int32_t scratch_capacity,39int32_t* result_capacity) override;4041private:42CharString& dest_;43};4445// CharString doesn't provide the public API that StringByteSink requires a46// string class to have so this template specialization replaces the default47// implementation of StringByteSink<CharString> with CharStringByteSink.48template<>49class StringByteSink<CharString> : public CharStringByteSink {50public:51StringByteSink(CharString* dest) : CharStringByteSink(dest) { }52StringByteSink(CharString* dest, int32_t /*initialAppendCapacity*/) : CharStringByteSink(dest) { }53};5455class U_COMMON_API ByteSinkUtil {56public:57ByteSinkUtil() = delete; // all static5859/** (length) bytes were mapped to valid (s16, s16Length). */60static UBool appendChange(int32_t length,61const char16_t *s16, int32_t s16Length,62ByteSink &sink, Edits *edits, UErrorCode &errorCode);6364/** The bytes at [s, limit[ were mapped to valid (s16, s16Length). */65static UBool appendChange(const uint8_t *s, const uint8_t *limit,66const char16_t *s16, int32_t s16Length,67ByteSink &sink, Edits *edits, UErrorCode &errorCode);6869/** (length) bytes were mapped/changed to valid code point c. */70static void appendCodePoint(int32_t length, UChar32 c, ByteSink &sink, Edits *edits = nullptr);7172/** The few bytes at [src, nextSrc[ were mapped/changed to valid code point c. */73static inline void appendCodePoint(const uint8_t *src, const uint8_t *nextSrc, UChar32 c,74ByteSink &sink, Edits *edits = nullptr) {75appendCodePoint(static_cast<int32_t>(nextSrc - src), c, sink, edits);76}7778/** Append the two-byte character (U+0080..U+07FF). */79static void appendTwoBytes(UChar32 c, ByteSink &sink);8081static UBool appendUnchanged(const uint8_t *s, int32_t length,82ByteSink &sink, uint32_t options, Edits *edits,83UErrorCode &errorCode) {84if (U_FAILURE(errorCode)) { return false; }85if (length > 0) { appendNonEmptyUnchanged(s, length, sink, options, edits); }86return true;87}8889static UBool appendUnchanged(const uint8_t *s, const uint8_t *limit,90ByteSink &sink, uint32_t options, Edits *edits,91UErrorCode &errorCode);9293/**94* Calls a lambda that writes to a ByteSink with a CheckedArrayByteSink95* and then returns through u_terminateChars(), in order to implement96* the classic ICU4C C API writing to a fix sized buffer on top of a97* contemporary C++ API.98*99* @param buffer receiving buffer100* @param capacity capacity of receiving buffer101* @param lambda that gets called with the sink as an argument102* @param status set to U_BUFFER_OVERFLOW_ERROR on overflow103* @return number of bytes written, or needed (in case of overflow)104* @internal105*/106template <typename F,107typename = std::enable_if_t<108std::is_invocable_r_v<void, F, ByteSink&, UErrorCode&>>>109static int32_t viaByteSinkToTerminatedChars(char* buffer, int32_t capacity,110F&& lambda,111UErrorCode& status) {112if (U_FAILURE(status)) { return 0; }113CheckedArrayByteSink sink(buffer, capacity);114lambda(sink, status);115if (U_FAILURE(status)) { return 0; }116117int32_t reslen = sink.NumberOfBytesAppended();118119if (sink.Overflowed()) {120status = U_BUFFER_OVERFLOW_ERROR;121return reslen;122}123124return u_terminateChars(buffer, capacity, reslen, &status);125}126127/**128* Calls a lambda that writes to a ByteSink with a CharStringByteSink and129* then returns a CharString, in order to implement a contemporary C++ API130* on top of a C/C++ compatibility ByteSink API.131*132* @param lambda that gets called with the sink as an argument133* @param status to check and report134* @return the resulting string, or an empty string (in case of error)135* @internal136*/137template <typename F,138typename = std::enable_if_t<139std::is_invocable_r_v<void, F, ByteSink&, UErrorCode&>>>140static CharString viaByteSinkToCharString(F&& lambda, UErrorCode& status) {141if (U_FAILURE(status)) { return {}; }142CharString result;143CharStringByteSink sink(&result);144lambda(sink, status);145return result;146}147148private:149static void appendNonEmptyUnchanged(const uint8_t *s, int32_t length,150ByteSink &sink, uint32_t options, Edits *edits);151};152153U_NAMESPACE_END154155#endif //BYTESINKUTIL_H156157158