string_help.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 "../api_core.h"
33 #include <vector>
34 
35 #ifdef __cplusplus_cli
36 #pragma managed(push, on)
37 #include <vcclr.h>
38 #endif
39 
40 namespace clan
41 {
44 
46 class CL_API_CORE StringHelp
47 {
50 
51 public:
52  static std::vector<std::string> split_text(
53  const std::string &text,
54  const std::string &split_string,
55  bool skip_empty = true);
56 
61  static std::string wchar_to_utf8(wchar_t value);
62 
67  static std::string unicode_to_utf8(unsigned int value);
68 
74  static std::string trim(const std::string &text);
75 
83  static int compare(const std::string &a, const std::string &b, bool case_insensitive = false);
84 
92  static int compare(const std::wstring &a, const std::wstring &b, bool case_insensitive = false);
93 
99  static std::string text_to_upper(const std::string &s);
100 
106  static std::string local8_to_upper(const std::string &s);
107 
113  static std::wstring ucs2_to_upper(const std::wstring &s);
114 
120  static std::string text_to_lower(const std::string &s);
121 
127  static std::string local8_to_lower(const std::string &s);
128 
134  static std::wstring ucs2_to_lower(const std::wstring &s);
135 
142  static std::string float_to_text(float value, int num_decimal_places=6);
143 
150  static std::string float_to_local8(float value, int num_decimal_places=6);
151 
158  static std::wstring float_to_ucs2(float value, int num_decimal_places=6);
159 
165  static float text_to_float(const std::string &value);
166 
172  static float local8_to_float(const std::string &value);
173 
179  static float ucs2_to_float(const std::wstring &value);
180 
187  static std::string double_to_text(double value, int num_decimal_places=6);
188 
195  static std::string double_to_local8(double value, int num_decimal_places=6);
196 
203  static std::wstring double_to_ucs2(double value, int num_decimal_places=6);
204 
210  static double text_to_double(const std::string &value);
211 
217  static double local8_to_double(const std::string &value);
218 
224  static double ucs2_to_double(const std::wstring &value);
225 
231  static std::string int_to_text(int value);
232 
238  static std::string int_to_local8(int value);
239 
245  static std::wstring int_to_ucs2(int value);
246 
253  static int text_to_int(const std::string &value, int base = 10);
254 
261  static int local8_to_int(const std::string &value, int base = 10);
262 
269  static int ucs2_to_int(const std::wstring &value, int base = 10);
270 
276  static std::string uint_to_text(unsigned int value);
277 
283  static std::string uint_to_local8(unsigned int value);
284 
290  static std::wstring uint_to_ucs2(unsigned int value);
291 
292  static unsigned int text_to_uint(const std::string &value, int base = 10);
293 
294  static unsigned int local8_to_uint(const std::string &value, int base = 10);
295 
296  static unsigned int ucs2_to_uint(const std::wstring &value, int base = 10);
297 
303  static std::string ll_to_text(long long value);
304 
310  static std::string ll_to_local8(long long value);
311 
317  static std::wstring ll_to_ucs2(long long value);
318 
325  static long long text_to_ll(const std::string &value, int base = 10);
326 
333  static long long local8_to_ll(const std::string &value, int base = 10);
334 
341  static long long ucs2_to_ll(const std::wstring &value, int base = 10);
342 
348  static std::string ull_to_text(unsigned long long value);
349 
355  static std::string ull_to_local8(unsigned long long value);
356 
362  static std::wstring ull_to_ucs2(unsigned long long value);
363 
370  static unsigned long long text_to_ull(const std::string &value, int base = 10);
371 
378  static unsigned long long local8_to_ull(const std::string &value, int base = 10);
379 
386  static unsigned long long ucs2_to_ull(const std::wstring &value, int base = 10);
387 
393  static std::string bool_to_text(bool value);
394 
400  static std::string bool_to_local8(bool value);
401 
407  static std::wstring bool_to_ucs2(bool value);
408 
414  static bool text_to_bool(const std::string &value);
415 
421  static bool local8_to_bool(const std::string &value);
422 
428  static bool ucs2_to_bool(const std::wstring &value);
429 
435  static std::string text_to_cp437(const std::string &text);
436 
442  static std::string ucs2_to_cp437(const std::wstring &text);
443 
449  static std::string cp437_to_text(const std::string &cp437);
450 
456  static std::wstring cp437_to_ucs2(const std::string &cp437);
457 
463  static std::string text_to_local8(const std::string &text);
464 
470  static std::string text_to_utf8(const std::string &text);
471 
477  static std::string ucs2_to_latin1(const std::wstring &ucs2);
478 
484  static std::string ucs2_to_latin9(const std::wstring &ucs2);
485 
491  static std::string ucs2_to_local8(const std::wstring &ucs2);
492 
498  static std::string ucs2_to_utf8(const std::wstring &ucs2);
499 
505  static std::string local8_to_text(const std::string &local8);
506 
512  static std::string ucs2_to_text(const std::wstring &ucs2);
513 
519  static std::wstring latin1_to_ucs2(const std::string &latin1);
520 
526  static std::wstring latin9_to_ucs2(const std::string &latin9);
527 
533  static std::wstring local8_to_ucs2(const std::string &local8);
534 
540  static std::wstring utf8_to_ucs2(const std::string &utf8);
541 
547  static std::string utf8_to_text(const std::string &utf8);
548 
549  static std::string::size_type utf8_length(const std::string &str);
550 
551  enum BOMType
552  {
558  bom_utf8
559  };
560 
567  static BOMType detect_bom(const void *data, std::string::size_type length);
568 
569 #ifdef __cplusplus_cli
570  static std::string clr_to_text(const System::String ^str)
576  {
577  return ucs2_to_utf8(clr_to_ucs2(str));
578  }
579 
580  static System::String ^text_to_clr(const std::string &str)
581  {
582  return ucs2_to_clr(utf8_to_ucs2(str));
583  }
584 
590  static std::wstring clr_to_ucs2(const System::String ^str)
591  {
592  cli::pin_ptr<const wchar_t> chars = PtrToStringChars(str);
593  return std::wstring(chars);
594  }
595 
596  static System::String ^ucs2_to_clr(const std::wstring &str)
597  {
598  return gcnew System::String(str.c_str());
599  }
600 #endif
601 
605 
606 private:
607  static const char trailing_bytes_for_utf8[256];
608 
609  static const unsigned char bitmask_leadbyte_for_utf8[6];
611 };
612 
613 }
614 
615 #ifdef __cplusplus_cli
616 #pragma managed(pop)
617 #endif
618 
String helper functions.
Definition: string_help.h:46
Definition: string_help.h:554
Definition: string_help.h:553
BOMType
Definition: string_help.h:551
Definition: string_help.h:557
Definition: string_help.h:555
Definition: string_help.h:556