string_help.h Source File - ClanLib SDK
Description: string_help.h Source File

string_help.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 The ClanLib 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 ClanLib may link to may have additional
22** requirements or restrictions.
23**
24** File Author(s):
25**
26** Magnus Norddahl
27*/
28
29
30#pragma once
31
32#include <vector>
33
34#ifdef __cplusplus_cli
35#pragma managed(push, on)
36#include <vcclr.h>
37#endif
38
39namespace clan
40{
43
45 class StringHelp
46 {
47 public:
48 static std::vector<std::string> split_text(
49 const std::string &text,
50 const std::string &split_string,
51 bool skip_empty = true);
52
57 static std::string wchar_to_utf8(wchar_t value);
58
63 static std::string unicode_to_utf8(unsigned int value);
64
70 static std::string trim(const std::string &text);
71
76 static std::string remove_trailing_zeros(std::string text);
77 static std::wstring remove_trailing_zeros(std::wstring text);
78
86 static int compare(const std::string &a, const std::string &b, bool case_insensitive = false);
87
95 static int compare(const std::wstring &a, const std::wstring &b, bool case_insensitive = false);
96
102 static std::string text_to_upper(const std::string &s);
103
109 static std::string local8_to_upper(const std::string &s);
110
116 static std::wstring ucs2_to_upper(const std::wstring &s);
117
123 static std::string text_to_lower(const std::string &s);
124
130 static std::string local8_to_lower(const std::string &s);
131
137 static std::wstring ucs2_to_lower(const std::wstring &s);
138
145 static std::string float_to_text(float value, int num_decimal_places = 6, bool remove_trailing_zeros = true);
146
153 static std::string float_to_local8(float value, int num_decimal_places = 6, bool remove_trailing_zeros = true);
154
161 static std::wstring float_to_ucs2(float value, int num_decimal_places = 6, bool remove_trailing_zeros = true);
162
168 static float text_to_float(const std::string &value);
169
175 static float local8_to_float(const std::string &value);
176
182 static float ucs2_to_float(const std::wstring &value);
183
190 static std::string double_to_text(double value, int num_decimal_places = 6);
191
198 static std::string double_to_local8(double value, int num_decimal_places = 6);
199
206 static std::wstring double_to_ucs2(double value, int num_decimal_places = 6);
207
213 static double text_to_double(const std::string &value);
214
220 static double local8_to_double(const std::string &value);
221
227 static double ucs2_to_double(const std::wstring &value);
228
234 static std::string int_to_text(int value);
235
241 static std::string int_to_local8(int value);
242
248 static std::wstring int_to_ucs2(int value);
249
256 static int text_to_int(const std::string &value, int base = 10);
257
264 static int local8_to_int(const std::string &value, int base = 10);
265
272 static int ucs2_to_int(const std::wstring &value, int base = 10);
273
279 static std::string uint_to_text(unsigned int value);
280
286 static std::string uint_to_local8(unsigned int value);
287
293 static std::wstring uint_to_ucs2(unsigned int value);
294
295 static unsigned int text_to_uint(const std::string &value, int base = 10);
296
297 static unsigned int local8_to_uint(const std::string &value, int base = 10);
298
299 static unsigned int ucs2_to_uint(const std::wstring &value, int base = 10);
300
306 static std::string ll_to_text(long long value);
307
313 static std::string ll_to_local8(long long value);
314
320 static std::wstring ll_to_ucs2(long long value);
321
328 static long long text_to_ll(const std::string &value, int base = 10);
329
336 static long long local8_to_ll(const std::string &value, int base = 10);
337
344 static long long ucs2_to_ll(const std::wstring &value, int base = 10);
345
351 static std::string ull_to_text(unsigned long long value);
352
358 static std::string ull_to_local8(unsigned long long value);
359
365 static std::wstring ull_to_ucs2(unsigned long long value);
366
373 static unsigned long long text_to_ull(const std::string &value, int base = 10);
374
381 static unsigned long long local8_to_ull(const std::string &value, int base = 10);
382
389 static unsigned long long ucs2_to_ull(const std::wstring &value, int base = 10);
390
396 static std::string bool_to_text(bool value);
397
403 static std::string bool_to_local8(bool value);
404
410 static std::wstring bool_to_ucs2(bool value);
411
417 static bool text_to_bool(const std::string &value);
418
424 static bool local8_to_bool(const std::string &value);
425
431 static bool ucs2_to_bool(const std::wstring &value);
432
438 static std::string text_to_cp437(const std::string &text);
439
445 static std::string ucs2_to_cp437(const std::wstring &text);
446
452 static std::string cp437_to_text(const std::string &cp437);
453
459 static std::wstring cp437_to_ucs2(const std::string &cp437);
460
466 static std::string ucs2_to_latin1(const std::wstring &ucs2);
467
473 static std::string ucs2_to_latin9(const std::wstring &ucs2);
474
480 static std::string ucs2_to_local8(const std::wstring &ucs2);
481
487 static std::string ucs2_to_utf8(const std::wstring &ucs2);
488
494 static std::string ucs2_to_text(const std::wstring &ucs2);
495
501 static std::wstring latin1_to_ucs2(const std::string &latin1);
502
508 static std::wstring latin9_to_ucs2(const std::string &latin9);
509
515 static std::wstring local8_to_ucs2(const std::string &local8);
516
522 static std::wstring utf8_to_ucs2(const std::string &utf8);
523
529 static std::string utf8_to_text(const std::string &utf8);
530
531 static std::string::size_type utf8_length(const std::string &str);
532
533 enum BOMType
534 {
535 bom_none,
536 bom_utf32_be,
537 bom_utf32_le,
538 bom_utf16_be,
539 bom_utf16_le,
540 bom_utf8
541 };
542
549 static BOMType detect_bom(const void *data, std::string::size_type length);
550
551#ifdef __cplusplus_cli
557 static std::string clr_to_text(const System::String ^str)
558 {
559 return ucs2_to_utf8(clr_to_ucs2(str));
560 }
561
562 static System::String ^text_to_clr(const std::string &str)
563 {
564 return ucs2_to_clr(utf8_to_ucs2(str));
565 }
566
572 static std::wstring clr_to_ucs2(const System::String ^str)
573 {
574 cli::pin_ptr<const wchar_t> chars = PtrToStringChars(str);
575 return std::wstring(chars);
576 }
577
578 static System::String ^ucs2_to_clr(const std::wstring &str)
579 {
580 return gcnew System::String(str.c_str());
581 }
582#endif
583
584 private:
585 static const char trailing_bytes_for_utf8[256];
586 static const unsigned char bitmask_leadbyte_for_utf8[6];
587 };
588
590}
591
592#ifdef __cplusplus_cli
593#pragma managed(pop)
594#endif
595
clan::StringHelp
String helper functions.
Definition string_help.h:46
clan::StringHelp::utf8_to_ucs2
static std::wstring utf8_to_ucs2(const std::string &utf8)
Utf8 to ucs2.
clan::StringHelp::text_to_upper
static std::string text_to_upper(const std::string &s)
Text to upper.
clan::StringHelp::ucs2_to_int
static int ucs2_to_int(const std::wstring &value, int base=10)
Ucs2 to int.
clan::StringHelp::detect_bom
static BOMType detect_bom(const void *data, std::string::size_type length)
Detect bom.
clan::StringHelp::ucs2_to_local8
static std::string ucs2_to_local8(const std::wstring &ucs2)
Ucs2 to local8.
clan::StringHelp::ucs2_to_ll
static long long ucs2_to_ll(const std::wstring &value, int base=10)
Ucs2 to long long.
clan::StringHelp::ucs2_to_utf8
static std::string ucs2_to_utf8(const std::wstring &ucs2)
Ucs2 to utf8.
clan::StringHelp::ucs2_to_lower
static std::wstring ucs2_to_lower(const std::wstring &s)
Ucs2 to lower.
clan::StringHelp::float_to_ucs2
static std::wstring float_to_ucs2(float value, int num_decimal_places=6, bool remove_trailing_zeros=true)
Float to ucs2.
clan::StringHelp::split_text
static std::vector< std::string > split_text(const std::string &text, const std::string &split_string, bool skip_empty=true)
clan::StringHelp::utf8_to_text
static std::string utf8_to_text(const std::string &utf8)
Utf8 to text.
clan::StringHelp::ucs2_to_cp437
static std::string ucs2_to_cp437(const std::wstring &text)
Ucs2 to cp437.
clan::StringHelp::unicode_to_utf8
static std::string unicode_to_utf8(unsigned int value)
unicode char to utf-8
clan::StringHelp::text_to_float
static float text_to_float(const std::string &value)
Text to float.
clan::StringHelp::local8_to_lower
static std::string local8_to_lower(const std::string &s)
Local8 to lower.
clan::StringHelp::local8_to_uint
static unsigned int local8_to_uint(const std::string &value, int base=10)
clan::StringHelp::uint_to_local8
static std::string uint_to_local8(unsigned int value)
Uint to local8.
clan::StringHelp::double_to_ucs2
static std::wstring double_to_ucs2(double value, int num_decimal_places=6)
Double to ucs2.
clan::StringHelp::ull_to_ucs2
static std::wstring ull_to_ucs2(unsigned long long value)
Unsigned long long to ucs2.
clan::StringHelp::compare
static int compare(const std::wstring &a, const std::wstring &b, bool case_insensitive=false)
Compare.
clan::StringHelp::latin9_to_ucs2
static std::wstring latin9_to_ucs2(const std::string &latin9)
Latin9 to ucs2.
clan::StringHelp::ll_to_text
static std::string ll_to_text(long long value)
Long long to text.
clan::StringHelp::text_to_ll
static long long text_to_ll(const std::string &value, int base=10)
Text to long long.
clan::StringHelp::local8_to_upper
static std::string local8_to_upper(const std::string &s)
Local8 to upper.
clan::StringHelp::text_to_double
static double text_to_double(const std::string &value)
Text to double.
clan::StringHelp::text_to_lower
static std::string text_to_lower(const std::string &s)
Text to lower.
clan::StringHelp::uint_to_text
static std::string uint_to_text(unsigned int value)
Uint to text.
clan::StringHelp::bool_to_local8
static std::string bool_to_local8(bool value)
Bool to local8.
clan::StringHelp::text_to_ull
static unsigned long long text_to_ull(const std::string &value, int base=10)
Text to unsigned long long.
clan::StringHelp::local8_to_ucs2
static std::wstring local8_to_ucs2(const std::string &local8)
Local8 to ucs2.
clan::StringHelp::ucs2_to_upper
static std::wstring ucs2_to_upper(const std::wstring &s)
Ucs2 to upper.
clan::StringHelp::ucs2_to_float
static float ucs2_to_float(const std::wstring &value)
Ucs2 to float.
clan::StringHelp::local8_to_ll
static long long local8_to_ll(const std::string &value, int base=10)
Local8 to long long.
clan::StringHelp::ull_to_text
static std::string ull_to_text(unsigned long long value)
Unsigned long long to text.
clan::StringHelp::remove_trailing_zeros
static std::wstring remove_trailing_zeros(std::wstring text)
clan::StringHelp::latin1_to_ucs2
static std::wstring latin1_to_ucs2(const std::string &latin1)
Latin1 to ucs2.
clan::StringHelp::trim
static std::string trim(const std::string &text)
Trim.
clan::StringHelp::local8_to_int
static int local8_to_int(const std::string &value, int base=10)
Local8 to int.
clan::StringHelp::ucs2_to_ull
static unsigned long long ucs2_to_ull(const std::wstring &value, int base=10)
Ucs2 to unsigned long long.
clan::StringHelp::ucs2_to_text
static std::string ucs2_to_text(const std::wstring &ucs2)
Ucs2 to text.
clan::StringHelp::uint_to_ucs2
static std::wstring uint_to_ucs2(unsigned int value)
Uint to ucs2.
clan::StringHelp::compare
static int compare(const std::string &a, const std::string &b, bool case_insensitive=false)
Compare.
clan::StringHelp::BOMType
BOMType
Definition string_help.h:534
clan::StringHelp::bom_utf32_le
@ bom_utf32_le
Definition string_help.h:537
clan::StringHelp::bom_none
@ bom_none
Definition string_help.h:535
clan::StringHelp::bom_utf16_le
@ bom_utf16_le
Definition string_help.h:539
clan::StringHelp::bom_utf8
@ bom_utf8
Definition string_help.h:540
clan::StringHelp::bom_utf16_be
@ bom_utf16_be
Definition string_help.h:538
clan::StringHelp::bom_utf32_be
@ bom_utf32_be
Definition string_help.h:536
clan::StringHelp::int_to_ucs2
static std::wstring int_to_ucs2(int value)
Int to ucs2.
clan::StringHelp::text_to_bool
static bool text_to_bool(const std::string &value)
Text to bool.
clan::StringHelp::wchar_to_utf8
static std::string wchar_to_utf8(wchar_t value)
Wide char to utf-8.
clan::StringHelp::remove_trailing_zeros
static std::string remove_trailing_zeros(std::string text)
Removes trailing zeros from floating point numbers.
clan::StringHelp::ll_to_local8
static std::string ll_to_local8(long long value)
Long long to local8.
clan::StringHelp::ull_to_local8
static std::string ull_to_local8(unsigned long long value)
Unsigned long long to local8.
clan::StringHelp::double_to_local8
static std::string double_to_local8(double value, int num_decimal_places=6)
Double to local8.
clan::StringHelp::bool_to_text
static std::string bool_to_text(bool value)
Bool to text.
clan::StringHelp::int_to_text
static std::string int_to_text(int value)
Int to text.
clan::StringHelp::text_to_int
static int text_to_int(const std::string &value, int base=10)
Text to int.
clan::StringHelp::ucs2_to_latin1
static std::string ucs2_to_latin1(const std::wstring &ucs2)
Ucs2 to latin1.
clan::StringHelp::ucs2_to_uint
static unsigned int ucs2_to_uint(const std::wstring &value, int base=10)
clan::StringHelp::float_to_local8
static std::string float_to_local8(float value, int num_decimal_places=6, bool remove_trailing_zeros=true)
Float to local8.
clan::StringHelp::utf8_length
static std::string::size_type utf8_length(const std::string &str)
clan::StringHelp::local8_to_ull
static unsigned long long local8_to_ull(const std::string &value, int base=10)
Local8 to unsigned long long.
clan::StringHelp::cp437_to_ucs2
static std::wstring cp437_to_ucs2(const std::string &cp437)
Cp437 to ucs2.
clan::StringHelp::ucs2_to_bool
static bool ucs2_to_bool(const std::wstring &value)
Ucs2 to bool.
clan::StringHelp::cp437_to_text
static std::string cp437_to_text(const std::string &cp437)
Cp437 to text.
clan::StringHelp::ll_to_ucs2
static std::wstring ll_to_ucs2(long long value)
Long long to ucs2.
clan::StringHelp::local8_to_double
static double local8_to_double(const std::string &value)
Local8 to double.
clan::StringHelp::ucs2_to_latin9
static std::string ucs2_to_latin9(const std::wstring &ucs2)
Ucs2 to latin9.
clan::StringHelp::double_to_text
static std::string double_to_text(double value, int num_decimal_places=6)
Double to text.
clan::StringHelp::local8_to_bool
static bool local8_to_bool(const std::string &value)
Local8 to bool.
clan::StringHelp::text_to_cp437
static std::string text_to_cp437(const std::string &text)
Text to cp437.
clan::StringHelp::ucs2_to_double
static double ucs2_to_double(const std::wstring &value)
Ucs2 to double.
clan::StringHelp::int_to_local8
static std::string int_to_local8(int value)
Int to local8.
clan::StringHelp::local8_to_float
static float local8_to_float(const std::string &value)
Local8 to float.
clan::StringHelp::bool_to_ucs2
static std::wstring bool_to_ucs2(bool value)
Bool to ucs2.
clan::StringHelp::text_to_uint
static unsigned int text_to_uint(const std::string &value, int base=10)
clan::StringHelp::float_to_text
static std::string float_to_text(float value, int num_decimal_places=6, bool remove_trailing_zeros=true)
Float to text.
clan
Definition clanapp.h:36
clan::StyleValueType::length
@ length
value is a keyword
Definition style_value_type.h:38
clan::Key::s
@ s
Definition keys.h:99
clan::Key::a
@ a
Definition keys.h:81
clan::Key::b
@ b
Definition keys.h:82