/** * A StringBuilder optimized for formatting. It implements the following key * features beyond a UnicodeString: * * <ol> * <li>Efficient prepend as well as append. * <li>Keeps track of Fields in an efficient manner. * </ol> * * See also FormattedValueStringBuilderImpl. * * @author sffc (Shane Carr)
*/ class U_I18N_API FormattedStringBuilder : public UMemory { private: staticconst int32_t DEFAULT_CAPACITY = 40;
template<typename T> union ValueOrHeapArray {
T value[DEFAULT_CAPACITY]; struct {
T *ptr;
int32_t capacity;
} heap;
};
// Convention: bottom 4 bits for field, top 4 bits for field category. // Field category 0 implies the number category so that the number field // literals can be directly passed as a Field type. // Exported as U_I18N_API so it can be used by other exports on Windows. struct U_I18N_API Field {
uint8_t bits;
/** Appends a UTF-16 code unit. */ inline int32_t appendChar16(char16_t codeUnit, Field field, UErrorCode& status) { // appendCodePoint handles both code units and code points. return insertCodePoint(fLength, codeUnit, field, status);
}
/** Inserts a UTF-16 code unit. Note: insert at index 0 is very efficient. */ inline int32_t insertChar16(int32_t index, char16_t codeUnit, Field field, UErrorCode& status) { // insertCodePoint handles both code units and code points. return insertCodePoint(index, codeUnit, field, status);
}
/** Inserts a Unicode code point. Note: insert at index 0 is very efficient. */
int32_t insertCodePoint(int32_t index, UChar32 codePoint, Field field, UErrorCode &status);
/** Inserts a string. Note: insert at index 0 is very efficient. */
int32_t insert(int32_t index, const UnicodeString &unistr, Field field, UErrorCode &status);
/** Inserts a substring. Note: insert at index 0 is very efficient. * * @param start Start index of the substring of unistr to be inserted. * @param end End index of the substring of unistr to be inserted (exclusive).
*/
int32_t insert(int32_t index, const UnicodeString &unistr, int32_t start, int32_t end, Field field,
UErrorCode &status);
/** Deletes a substring and then inserts a string at that same position. * Similar to JavaScript Array.prototype.splice(). * * @param startThis Start of the span to delete. * @param endThis End of the span to delete (exclusive). * @param unistr The string to insert at the deletion position. * @param startOther Start index of the substring of unistr to be inserted. * @param endOther End index of the substring of unistr to be inserted (exclusive).
*/
int32_t splice(int32_t startThis, int32_t endThis, const UnicodeString &unistr,
int32_t startOther, int32_t endOther, Field field, UErrorCode& status);
/** Inserts a formatted string. Note: insert at index 0 is very efficient. */
int32_t insert(int32_t index, const FormattedStringBuilder &other, UErrorCode &status);
/** * Ensures that the string buffer contains a NUL terminator. The NUL terminator does * not count toward the string length. Any further changes to the string (insert or * append) may invalidate the NUL terminator. * * You should call this method after the formatted string is completely built if you * plan to return a pointer to the string from a C API.
*/ void writeTerminator(UErrorCode& status);
/** * Gets a "safe" UnicodeString that can be used even after the FormattedStringBuilder is destructed.
*/
UnicodeString toUnicodeString() const;
/** * Gets an "unsafe" UnicodeString that is valid only as long as the FormattedStringBuilder is alive and * unchanged. Slightly faster than toUnicodeString().
*/
UnicodeString toTempUnicodeString() const;
static_assert( // std::is_pod<> is deprecated.
std::is_standard_layout_v<FormattedStringBuilder::Field> &&
std::is_trivial_v<FormattedStringBuilder::Field>, "Field should be a POD type for efficient initialization");
/** * Internal constant for the undefined field for use in FormattedStringBuilder.
*/
constexpr FormattedStringBuilder::Field kUndefinedField = {UFIELD_CATEGORY_UNDEFINED, 0};
/** * Internal field to signal "numeric" when fields are not supported in NumberFormat.
*/
constexpr FormattedStringBuilder::Field kGeneralNumericField = {UFIELD_CATEGORY_UNDEFINED, 1};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.