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