A Unicode string class, which is a basic UTF-8 aware wrapper around std::string. Provides methods for accessing UTF-32 "Char" type, which provides access to each individual code point. Printing, hashing, copying, and in-order access should be basically as fast as std::string, but the more complex string processing methods may be much worse. All case sensitive / insensitive functionality is base
| 27 | // case insensitivity is really only appropriate for code / script processing, |
| 28 | // not for general strings. |
| 29 | class String { |
| 30 | public: |
| 31 | typedef Utf32Type Char; |
| 32 | |
| 33 | // std::basic_string equivalent that guarantees const access time for |
| 34 | // operator[], etc |
| 35 | typedef std::basic_string<Char> WideString; |
| 36 | |
| 37 | typedef U8ToU32Iterator<std::string::const_iterator> const_iterator; |
| 38 | typedef Char value_type; |
| 39 | typedef value_type const& const_reference; |
| 40 | |
| 41 | enum CaseSensitivity { |
| 42 | CaseSensitive, |
| 43 | CaseInsensitive |
| 44 | }; |
| 45 | |
| 46 | // Space, horizontal tab, newline, carriage return, and BOM / ZWNBSP |
| 47 | static bool isSpace(Char c); |
| 48 | static bool isAsciiNumber(Char c); |
| 49 | static bool isAsciiLetter(Char c); |
| 50 | |
| 51 | // These methods only actually work on unicode characters below 127, i.e. |
| 52 | // ASCII subset. |
| 53 | static Char toLower(Char c); |
| 54 | static Char toUpper(Char c); |
| 55 | static bool charEqual(Char c1, Char c2, CaseSensitivity cs); |
| 56 | |
| 57 | // Join two strings together with a joiner, so that only one instance of the |
| 58 | // joiner is in between the left and right strings. For example, joins "foo" |
| 59 | // and "bar" with "?" to produce "foo?bar". Gets rid of repeat joiners, so |
| 60 | // "foo?" and "?bar" with "?" also becomes "foo?bar". Also, if left or right |
| 61 | // is empty, does not add a joiner, for example "" and "baz" joined with "?" |
| 62 | // produces "baz". |
| 63 | static String joinWith(String const& join, String const& left, String const& right); |
| 64 | template <typename... StringType> |
| 65 | static String joinWith(String const& join, String const& first, String const& second, String const& third, StringType const&... rest); |
| 66 | |
| 67 | String(); |
| 68 | String(String const& s); |
| 69 | String(String&& s); |
| 70 | |
| 71 | // These assume utf8 input |
| 72 | String(char const* s); |
| 73 | String(char const* s, size_t n); |
| 74 | String(std::string const& s); |
| 75 | String(std::string&& s); |
| 76 | |
| 77 | String(std::wstring const& s); |
| 78 | String(Char const* s); |
| 79 | String(Char const* s, size_t n); |
| 80 | String(Char c, size_t n); |
| 81 | |
| 82 | explicit String(Char c); |
| 83 | |
| 84 | // const& to internal utf8 data |
| 85 | std::string const& utf8() const; |
| 86 | std::string takeUtf8(); |