string_help.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 
39 namespace clan
40 {
43 
46 {
49 
50 public:
51  static std::vector<std::string> split_text(
52  const std::string &text,
53  const std::string &split_string,
54  bool skip_empty = true);
55 
60  static std::string wchar_to_utf8(wchar_t value);
61 
66  static std::string unicode_to_utf8(unsigned int value);
67 
73  static std::string trim(const std::string &text);
74 
79  static std::string remove_trailing_zeros(std::string text);
80  static std::wstring remove_trailing_zeros(std::wstring text);
81 
89  static int compare(const std::string &a, const std::string &b, bool case_insensitive = false);
90 
98  static int compare(const std::wstring &a, const std::wstring &b, bool case_insensitive = false);
99 
105  static std::string text_to_upper(const std::string &s);
106 
112  static std::string local8_to_upper(const std::string &s);
113 
119  static std::wstring ucs2_to_upper(const std::wstring &s);
120 
126  static std::string text_to_lower(const std::string &s);
127 
133  static std::string local8_to_lower(const std::string &s);
134 
140  static std::wstring ucs2_to_lower(const std::wstring &s);
141 
148  static std::string float_to_text(float value, int num_decimal_places = 6, bool remove_trailing_zeros = true);
149 
156  static std::string float_to_local8(float value, int num_decimal_places = 6, bool remove_trailing_zeros = true);
157 
164  static std::wstring float_to_ucs2(float value, int num_decimal_places = 6, bool remove_trailing_zeros = true);
165 
171  static float text_to_float(const std::string &value);
172 
178  static float local8_to_float(const std::string &value);
179 
185  static float ucs2_to_float(const std::wstring &value);
186 
193  static std::string double_to_text(double value, int num_decimal_places=6);
194 
201  static std::string double_to_local8(double value, int num_decimal_places=6);
202 
209  static std::wstring double_to_ucs2(double value, int num_decimal_places=6);
210 
216  static double text_to_double(const std::string &value);
217 
223  static double local8_to_double(const std::string &value);
224 
230  static double ucs2_to_double(const std::wstring &value);
231 
237  static std::string int_to_text(int value);
238 
244  static std::string int_to_local8(int value);
245 
251  static std::wstring int_to_ucs2(int value);
252 
259  static int text_to_int(const std::string &value, int base = 10);
260 
267  static int local8_to_int(const std::string &value, int base = 10);
268 
275  static int ucs2_to_int(const std::wstring &value, int base = 10);
276 
282  static std::string uint_to_text(unsigned int value);
283 
289  static std::string uint_to_local8(unsigned int value);
290 
296  static std::wstring uint_to_ucs2(unsigned int value);
297 
298  static unsigned int text_to_uint(const std::string &value, int base = 10);
299 
300  static unsigned int local8_to_uint(const std::string &value, int base = 10);
301 
302  static unsigned int ucs2_to_uint(const std::wstring &value, int base = 10);
303 
309  static std::string ll_to_text(long long value);
310 
316  static std::string ll_to_local8(long long value);
317 
323  static std::wstring ll_to_ucs2(long long value);
324 
331  static long long text_to_ll(const std::string &value, int base = 10);
332 
339  static long long local8_to_ll(const std::string &value, int base = 10);
340 
347  static long long ucs2_to_ll(const std::wstring &value, int base = 10);
348 
354  static std::string ull_to_text(unsigned long long value);
355 
361  static std::string ull_to_local8(unsigned long long value);
362 
368  static std::wstring ull_to_ucs2(unsigned long long value);
369 
376  static unsigned long long text_to_ull(const std::string &value, int base = 10);
377 
384  static unsigned long long local8_to_ull(const std::string &value, int base = 10);
385 
392  static unsigned long long ucs2_to_ull(const std::wstring &value, int base = 10);
393 
399  static std::string bool_to_text(bool value);
400 
406  static std::string bool_to_local8(bool value);
407 
413  static std::wstring bool_to_ucs2(bool value);
414 
420  static bool text_to_bool(const std::string &value);
421 
427  static bool local8_to_bool(const std::string &value);
428 
434  static bool ucs2_to_bool(const std::wstring &value);
435 
441  static std::string text_to_cp437(const std::string &text);
442 
448  static std::string ucs2_to_cp437(const std::wstring &text);
449 
455  static std::string cp437_to_text(const std::string &cp437);
456 
462  static std::wstring cp437_to_ucs2(const std::string &cp437);
463 
469  static std::string text_to_local8(const std::string &text);
470 
476  static std::string text_to_utf8(const std::string &text);
477 
483  static std::string ucs2_to_latin1(const std::wstring &ucs2);
484 
490  static std::string ucs2_to_latin9(const std::wstring &ucs2);
491 
497  static std::string ucs2_to_local8(const std::wstring &ucs2);
498 
504  static std::string ucs2_to_utf8(const std::wstring &ucs2);
505 
511  static std::string local8_to_text(const std::string &local8);
512 
518  static std::string ucs2_to_text(const std::wstring &ucs2);
519 
525  static std::wstring latin1_to_ucs2(const std::string &latin1);
526 
532  static std::wstring latin9_to_ucs2(const std::string &latin9);
533 
539  static std::wstring local8_to_ucs2(const std::string &local8);
540 
546  static std::wstring utf8_to_ucs2(const std::string &utf8);
547 
553  static std::string utf8_to_text(const std::string &utf8);
554 
555  static std::string::size_type utf8_length(const std::string &str);
556 
557  enum BOMType
558  {
564  bom_utf8
565  };
566 
573  static BOMType detect_bom(const void *data, std::string::size_type length);
574 
575 #ifdef __cplusplus_cli
576  static std::string clr_to_text(const System::String ^str)
582  {
583  return ucs2_to_utf8(clr_to_ucs2(str));
584  }
585 
586  static System::String ^text_to_clr(const std::string &str)
587  {
588  return ucs2_to_clr(utf8_to_ucs2(str));
589  }
590 
596  static std::wstring clr_to_ucs2(const System::String ^str)
597  {
598  cli::pin_ptr<const wchar_t> chars = PtrToStringChars(str);
599  return std::wstring(chars);
600  }
601 
602  static System::String ^ucs2_to_clr(const std::wstring &str)
603  {
604  return gcnew System::String(str.c_str());
605  }
606 #endif
607 
611 
612 private:
613  static const char trailing_bytes_for_utf8[256];
614 
615  static const unsigned char bitmask_leadbyte_for_utf8[6];
617 };
618 
619 }
620 
621 #ifdef __cplusplus_cli
622 #pragma managed(pop)
623 #endif
624 
static unsigned long long text_to_ull(const std::string &value, int base=10)
Text to unsigned long long.
static bool ucs2_to_bool(const std::wstring &value)
Ucs2 to bool.
static std::wstring latin1_to_ucs2(const std::string &latin1)
Latin1 to ucs2.
@ bom_utf8
Definition: string_help.h:564
static std::string bool_to_local8(bool value)
Bool to local8.
static float text_to_float(const std::string &value)
Text to float.
static std::wstring ull_to_ucs2(unsigned long long value)
Unsigned long long to ucs2.
static std::string text_to_cp437(const std::string &text)
Text to cp437.
static std::string uint_to_text(unsigned int value)
Uint to text.
static double local8_to_double(const std::string &value)
Local8 to double.
static int text_to_int(const std::string &value, int base=10)
Text to int.
static std::string local8_to_lower(const std::string &s)
Local8 to lower.
@ bom_utf32_le
Definition: string_help.h:561
static std::string ull_to_text(unsigned long long value)
Unsigned long long to text.
static float local8_to_float(const std::string &value)
Local8 to float.
static std::string local8_to_text(const std::string &local8)
Local8 to text.
static std::wstring ucs2_to_upper(const std::wstring &s)
Ucs2 to upper.
static std::string text_to_local8(const std::string &text)
Text to local8.
static int compare(const std::wstring &a, const std::wstring &b, bool case_insensitive=false)
Compare.
static bool text_to_bool(const std::string &value)
Text to bool.
static std::string float_to_local8(float value, int num_decimal_places=6, bool remove_trailing_zeros=true)
Float to local8.
static std::string cp437_to_text(const std::string &cp437)
Cp437 to text.
static std::wstring local8_to_ucs2(const std::string &local8)
Local8 to ucs2.
static unsigned int local8_to_uint(const std::string &value, int base=10)
static std::wstring ucs2_to_lower(const std::wstring &s)
Ucs2 to lower.
static std::string float_to_text(float value, int num_decimal_places=6, bool remove_trailing_zeros=true)
Float to text.
static double ucs2_to_double(const std::wstring &value)
Ucs2 to double.
@ bom_utf16_le
Definition: string_help.h:563
static std::wstring bool_to_ucs2(bool value)
Bool to ucs2.
static std::string ucs2_to_text(const std::wstring &ucs2)
Ucs2 to text.
static std::string trim(const std::string &text)
Trim.
static std::string text_to_upper(const std::string &s)
Text to upper.
static long long local8_to_ll(const std::string &value, int base=10)
Local8 to long long.
static std::wstring int_to_ucs2(int value)
Int to ucs2.
static std::string bool_to_text(bool value)
Bool to text.
static std::wstring remove_trailing_zeros(std::wstring text)
@ bom_utf16_be
Definition: string_help.h:562
static std::string ucs2_to_latin9(const std::wstring &ucs2)
Ucs2 to latin9.
static std::string remove_trailing_zeros(std::string text)
Removes trailing zeros from floating point numbers.
static std::string ucs2_to_cp437(const std::wstring &text)
Ucs2 to cp437.
static std::string uint_to_local8(unsigned int value)
Uint to local8.
static std::string unicode_to_utf8(unsigned int value)
unicode char to utf-8
static std::string ucs2_to_latin1(const std::wstring &ucs2)
Ucs2 to latin1.
static std::wstring utf8_to_ucs2(const std::string &utf8)
Utf8 to ucs2.
static int compare(const std::string &a, const std::string &b, bool case_insensitive=false)
Compare.
static unsigned long long ucs2_to_ull(const std::wstring &value, int base=10)
Ucs2 to unsigned long long.
static long long ucs2_to_ll(const std::wstring &value, int base=10)
Ucs2 to long long.
static std::wstring uint_to_ucs2(unsigned int value)
Uint to ucs2.
static int ucs2_to_int(const std::wstring &value, int base=10)
Ucs2 to int.
static unsigned int ucs2_to_uint(const std::wstring &value, int base=10)
static unsigned int text_to_uint(const std::string &value, int base=10)
static std::string ucs2_to_local8(const std::wstring &ucs2)
Ucs2 to local8.
static std::wstring double_to_ucs2(double value, int num_decimal_places=6)
Double to ucs2.
Definition: clanapp.h:36
static std::wstring cp437_to_ucs2(const std::string &cp437)
Cp437 to ucs2.
static std::string int_to_local8(int value)
Int to local8.
String helper functions.
Definition: string_help.h:46
static std::string::size_type utf8_length(const std::string &str)
BOMType
Definition: string_help.h:558
static std::string double_to_text(double value, int num_decimal_places=6)
Double to text.
static std::string wchar_to_utf8(wchar_t value)
Wide char to utf-8.
static std::string ll_to_text(long long value)
Long long to text.
static std::string text_to_lower(const std::string &s)
Text to lower.
static std::string double_to_local8(double value, int num_decimal_places=6)
Double to local8.
static std::string local8_to_upper(const std::string &s)
Local8 to upper.
static std::wstring latin9_to_ucs2(const std::string &latin9)
Latin9 to ucs2.
static double text_to_double(const std::string &value)
Text to double.
static std::string text_to_utf8(const std::string &text)
Text to utf8.
static std::string ull_to_local8(unsigned long long value)
Unsigned long long to local8.
static long long text_to_ll(const std::string &value, int base=10)
Text to long long.
static float ucs2_to_float(const std::wstring &value)
Ucs2 to float.
static std::string ll_to_local8(long long value)
Long long to local8.
static bool local8_to_bool(const std::string &value)
Local8 to bool.
static std::wstring float_to_ucs2(float value, int num_decimal_places=6, bool remove_trailing_zeros=true)
Float to ucs2.
static std::string int_to_text(int value)
Int to text.
@ bom_utf32_be
Definition: string_help.h:560
static std::vector< std::string > split_text(const std::string &text, const std::string &split_string, bool skip_empty=true)
@ bom_none
Definition: string_help.h:559
static std::string ucs2_to_utf8(const std::wstring &ucs2)
Ucs2 to utf8.
static std::wstring ll_to_ucs2(long long value)
Long long to ucs2.
static unsigned long long local8_to_ull(const std::string &value, int base=10)
Local8 to unsigned long long.
static int local8_to_int(const std::string &value, int base=10)
Local8 to int.
static std::string utf8_to_text(const std::string &utf8)
Utf8 to text.
static BOMType detect_bom(const void *data, std::string::size_type length)
Detect bom.