/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_string_util.cpp

http://github.com/xbmc/xbmc · C++ · 264 lines · 168 code · 32 blank · 64 comment · 60 complexity · dff3f7621a001913d60f39e304a0a731 MD5 · raw file

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. #include "as_config.h"
  24. #include <stdarg.h> // va_list, va_start(), etc
  25. #include <stdlib.h> // strtod(), strtol()
  26. #include <stdio.h> // _vsnprintf()
  27. #include <string.h> // some compilers declare memcpy() here
  28. #include <locale.h> // setlocale()
  29. #if !defined(AS_NO_MEMORY_H)
  30. #include <memory.h>
  31. #endif
  32. #include "as_string.h"
  33. #include "as_string_util.h"
  34. BEGIN_AS_NAMESPACE
  35. double asStringScanDouble(const char *string, size_t *numScanned)
  36. {
  37. char *end;
  38. // WinCE doesn't have setlocale. Some quick testing on my current platform
  39. // still manages to parse the numbers such as "3.14" even if the decimal for the
  40. // locale is ",".
  41. #if !defined(_WIN32_WCE) && !defined(ANDROID)
  42. // Set the locale to C so that we are guaranteed to parse the float value correctly
  43. asCString orig = setlocale(LC_NUMERIC, 0);
  44. setlocale(LC_NUMERIC, "C");
  45. #endif
  46. double res = strtod(string, &end);
  47. #if !defined(_WIN32_WCE) && !defined(ANDROID)
  48. // Restore the locale
  49. setlocale(LC_NUMERIC, orig.AddressOf());
  50. #endif
  51. if( numScanned )
  52. *numScanned = end - string;
  53. return res;
  54. }
  55. asQWORD asStringScanUInt64(const char *string, int base, size_t *numScanned)
  56. {
  57. asASSERT(base == 10 || base == 16);
  58. const char *end = string;
  59. asQWORD res = 0;
  60. if( base == 10 )
  61. {
  62. while( *end >= '0' && *end <= '9' )
  63. {
  64. res *= 10;
  65. res += *end++ - '0';
  66. }
  67. }
  68. else if( base == 16 )
  69. {
  70. while( (*end >= '0' && *end <= '9') ||
  71. (*end >= 'a' && *end <= 'f') ||
  72. (*end >= 'A' && *end <= 'F') )
  73. {
  74. res *= 16;
  75. if( *end >= '0' && *end <= '9' )
  76. res += *end++ - '0';
  77. else if( *end >= 'a' && *end <= 'f' )
  78. res += *end++ - 'a' + 10;
  79. else if( *end >= 'A' && *end <= 'F' )
  80. res += *end++ - 'A' + 10;
  81. }
  82. }
  83. if( numScanned )
  84. *numScanned = end - string;
  85. return res;
  86. }
  87. //
  88. // The function will encode the unicode code point into the outEncodedBuffer, and then
  89. // return the length of the encoded value. If the input value is not a valid unicode code
  90. // point, then the function will return -1.
  91. //
  92. // This function is taken from the AngelCode ToolBox.
  93. //
  94. int asStringEncodeUTF8(unsigned int value, char *outEncodedBuffer)
  95. {
  96. unsigned char *buf = (unsigned char*)outEncodedBuffer;
  97. int length = -1;
  98. if( value <= 0x7F )
  99. {
  100. buf[0] = static_cast<unsigned char>(value);
  101. return 1;
  102. }
  103. else if( value >= 0x80 && value <= 0x7FF )
  104. {
  105. // Encode it with 2 characters
  106. buf[0] = static_cast<unsigned char>(0xC0 + (value >> 6));
  107. length = 2;
  108. }
  109. else if( (value >= 0x800 && value <= 0xD7FF) || (value >= 0xE000 && value <= 0xFFFF) )
  110. {
  111. // Note: Values 0xD800 to 0xDFFF are not valid unicode characters
  112. buf[0] = static_cast<unsigned char>(0xE0 + (value >> 12));
  113. length = 3;
  114. }
  115. else if( value >= 0x10000 && value <= 0x10FFFF )
  116. {
  117. buf[0] = static_cast<unsigned char>(0xF0 + (value >> 18));
  118. length = 4;
  119. }
  120. int n = length-1;
  121. for( ; n > 0; n-- )
  122. {
  123. buf[n] = static_cast<unsigned char>(0x80 + (value & 0x3F));
  124. value >>= 6;
  125. }
  126. return length;
  127. }
  128. //
  129. // The function will decode an UTF8 character and return the unicode code point.
  130. // outLength will receive the number of bytes that were decoded.
  131. //
  132. // This function is taken from the AngelCode ToolBox.
  133. //
  134. int asStringDecodeUTF8(const char *encodedBuffer, unsigned int *outLength)
  135. {
  136. const unsigned char *buf = (const unsigned char*)encodedBuffer;
  137. int value = 0;
  138. int length = -1;
  139. unsigned char byte = buf[0];
  140. if( (byte & 0x80) == 0 )
  141. {
  142. // This is the only byte
  143. if( outLength ) *outLength = 1;
  144. return byte;
  145. }
  146. else if( (byte & 0xE0) == 0xC0 )
  147. {
  148. // There is one more byte
  149. value = int(byte & 0x1F);
  150. length = 2;
  151. // The value at this moment must not be less than 2, because
  152. // that should have been encoded with one byte only.
  153. if( value < 2 )
  154. length = -1;
  155. }
  156. else if( (byte & 0xF0) == 0xE0 )
  157. {
  158. // There are two more bytes
  159. value = int(byte & 0x0F);
  160. length = 3;
  161. }
  162. else if( (byte & 0xF8) == 0xF0 )
  163. {
  164. // There are three more bytes
  165. value = int(byte & 0x07);
  166. length = 4;
  167. }
  168. int n = 1;
  169. for( ; n < length; n++ )
  170. {
  171. byte = buf[n];
  172. if( (byte & 0xC0) == 0x80 )
  173. value = (value << 6) + int(byte & 0x3F);
  174. else
  175. break;
  176. }
  177. if( n == length )
  178. {
  179. if( outLength ) *outLength = (unsigned)length;
  180. return value;
  181. }
  182. // The byte sequence isn't a valid UTF-8 byte sequence.
  183. return -1;
  184. }
  185. //
  186. // The function will encode the unicode code point into the outEncodedBuffer, and then
  187. // return the length of the encoded value. If the input value is not a valid unicode code
  188. // point, then the function will return -1.
  189. //
  190. // This function is taken from the AngelCode ToolBox.
  191. //
  192. int asStringEncodeUTF16(unsigned int value, char *outEncodedBuffer)
  193. {
  194. if( value < 0x10000 )
  195. {
  196. #ifndef AS_BIG_ENDIAN
  197. outEncodedBuffer[0] = (value & 0xFF);
  198. outEncodedBuffer[1] = ((value >> 8) & 0xFF);
  199. #else
  200. outEncodedBuffer[1] = (value & 0xFF);
  201. outEncodedBuffer[0] = ((value >> 8) & 0xFF);
  202. #endif
  203. return 2;
  204. }
  205. else
  206. {
  207. value -= 0x10000;
  208. int surrogate1 = ((value >> 10) & 0x3FF) + 0xD800;
  209. int surrogate2 = (value & 0x3FF) + 0xDC00;
  210. #ifndef AS_BIG_ENDIAN
  211. outEncodedBuffer[0] = (surrogate1 & 0xFF);
  212. outEncodedBuffer[1] = ((surrogate1 >> 8) & 0xFF);
  213. outEncodedBuffer[2] = (surrogate2 & 0xFF);
  214. outEncodedBuffer[3] = ((surrogate2 >> 8) & 0xFF);
  215. #else
  216. outEncodedBuffer[1] = (surrogate1 & 0xFF);
  217. outEncodedBuffer[0] = ((surrogate1 >> 8) & 0xFF);
  218. outEncodedBuffer[3] = (surrogate2 & 0xFF);
  219. outEncodedBuffer[2] = ((surrogate2 >> 8) & 0xFF);
  220. #endif
  221. return 4;
  222. }
  223. }
  224. END_AS_NAMESPACE