text.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include <vector>
32 
33 namespace uicore
34 {
36  enum class ByteOrderMark
37  {
38  none,
39  utf32_be,
40  utf32_le,
41  utf16_be,
42  utf16_le,
43  utf8
44  };
45 
47  class Text
48  {
49  public:
51  static std::vector<std::string> split(const std::string &text, const std::string &split_string, bool skip_empty = true);
52 
54  static std::string trim(const std::string &text);
55 
61 
63  static bool equal_caseless(const std::string &a, const std::string &b);
64 
66  static bool less_caseless(const std::string &a, const std::string &b);
67 
69  static std::string to_upper(const std::string &s);
70 
72  static std::string to_lower(const std::string &s);
73 
75  static std::string from_wchar(wchar_t value);
76 
78  static std::string from_utf32(unsigned int value);
79 
81  static std::string from_utf16(const std::wstring &s);
82 
84  static std::wstring to_utf16(const std::string &s);
85 
87  static std::string to_string(unsigned char value, int base = 10, bool uppercase = false) { return to_string((unsigned int)value, base, uppercase); }
88  static std::string to_string(unsigned short value, int base = 10, bool uppercase = false) { return to_string((unsigned int)value, base, uppercase); }
89  static std::string to_string(unsigned int value, int base = 10, bool uppercase = false);
90  static std::string to_string(unsigned long long value);
91  static std::string to_string(char value, int base = 10, bool uppercase = false) { return to_string((int)value, base, uppercase); }
92  static std::string to_string(short value, int base = 10, bool uppercase = false) { return to_string((int)value, base, uppercase); }
93  static std::string to_string(int value, int base = 10, bool uppercase = false);
94  static std::string to_string(long long value);
95  static std::string to_string(float value, int digits = 6, bool remove_trailing_zeros = true);
96  static std::string to_string(double value, int digits = 6, bool remove_trailing_zeros = true);
97  static std::string to_string(bool value);
98 
100  static unsigned int parse_uint32(const std::string &value, int base = 10);
101  static int parse_int32(const std::string &value, int base = 10);
102  static unsigned long long parse_uint64(const std::string &value, int base = 10);
103  static long long parse_int64(const std::string &value, int base = 10);
104  static float parse_float(const std::string &value);
105  static double parse_double(const std::string &value);
106  static bool parse_bool(const std::string &value);
107 
109  static std::string::size_type char_length(const std::string &str);
110 
112  static ByteOrderMark detect_bom(const void *data, std::string::size_type length);
113  };
114 }
static std::string trim(const std::string &text)
Trim whitespace from front and back.
ByteOrderMark
Unicode byte order mark (BOM) types.
Definition: text.h:36
static std::string from_utf16(const std::wstring &s)
Convert from UTF-16 to UTF-8.
static std::string from_wchar(wchar_t value)
Convert UCS-2 code point to UTF-8.
static bool parse_bool(const std::string &value)
static std::string to_upper(const std::string &s)
Convert text to uppercase.
static float parse_float(const std::string &value)
static std::string to_lower(const std::string &s)
Convert text to lowercase.
static double parse_double(const std::string &value)
static std::string to_string(unsigned char value, int base=10, bool uppercase=false)
Formats a number.
Definition: text.h:87
static std::string::size_type char_length(const std::string &str)
Finds the number of characters in a UTF-8 string.
static int parse_int32(const std::string &value, int base=10)
static std::string from_utf32(unsigned int value)
Convert Unicode code point to UTF-8.
static unsigned long long parse_uint64(const std::string &value, int base=10)
static std::string to_string(short value, int base=10, bool uppercase=false)
Definition: text.h:92
static ByteOrderMark detect_bom(const void *data, std::string::size_type length)
Searches for a byte order mark.
static unsigned int parse_uint32(const std::string &value, int base=10)
Parse a number.
static std::wstring to_utf16(const std::string &s)
Convert from UTF-8 to UTF-16.
static std::vector< std::string > split(const std::string &text, const std::string &split_string, bool skip_empty=true)
Split text into a list.
static long long parse_int64(const std::string &value, int base=10)
static std::string to_string(unsigned short value, int base=10, bool uppercase=false)
Definition: text.h:88
static std::string remove_trailing_zeros(std::string text)
Removes trailing zeros from floating point numbers.
Definition: Application/application.h:35
static bool equal_caseless(const std::string &a, const std::string &b)
Returns true if equal when doing a character case-insensitive comparison.
static std::string to_string(char value, int base=10, bool uppercase=false)
Definition: text.h:91
static bool less_caseless(const std::string &a, const std::string &b)
Returns true if less when doing a character case-insensitive comparison.
Text string functions.
Definition: text.h:47