| 1234567891011121314151617181920212223242526272829303132333435 |
- /**
- * Utility functions for managing strings, specifically because C++'s
- * std::string/std::regex is not well suited for UTF8 comprehensions.
- */
- #pragma once
- #include <jvalidate/_config.h>
- #include <cstring>
- #if JVALIDATE_HAS_ICU
- #include <unicode/brkiter.h>
- #include <unicode/unistr.h>
- #endif
- namespace jvalidate::detail {
- /**
- * @brief Calclates the string-length of the argument, treating multi-byte
- * characters and unicode graphemes as single characters (which std::string
- * cannot do).
- *
- * @param arg Any UTF8 compatible string (including a standard ASCII string)
- *
- * @returns A number no greater than arg.length(), depending on the number of
- * graphemes/codepoints in the string.
- */
- #if JVALIDATE_HAS_ICU
- inline size_t length(std::string_view arg) {
- icu::UnicodeString ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(arg));
- return ucs.countChar32();
- }
- #else
- inline size_t length(std::string_view arg) { return arg.length(); }
- #endif
- }
|