as_string_util.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 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. [email protected]
  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. int asCompareStrings(const char *str1, size_t len1, const char *str2, size_t len2)
  36. {
  37. if( len1 == 0 )
  38. {
  39. if( str2 == 0 || len2 == 0 ) return 0; // Equal
  40. return 1; // The other string is larger than this
  41. }
  42. if( str2 == 0 )
  43. {
  44. if( len1 == 0 )
  45. return 0; // Equal
  46. return -1; // The other string is smaller than this
  47. }
  48. if( len2 < len1 )
  49. {
  50. int result = memcmp(str1, str2, len2);
  51. if( result == 0 ) return -1; // The other string is smaller than this
  52. return result;
  53. }
  54. int result = memcmp(str1, str2, len1);
  55. if( result == 0 && len1 < len2 ) return 1; // The other string is larger than this
  56. return result;
  57. }
  58. double asStringScanDouble(const char *string, size_t *numScanned)
  59. {
  60. char *end;
  61. // WinCE doesn't have setlocale. Some quick testing on my current platform
  62. // still manages to parse the numbers such as "3.14" even if the decimal for the
  63. // locale is ",".
  64. #if !defined(_WIN32_WCE) && !defined(ANDROID)
  65. // Set the locale to C so that we are guaranteed to parse the float value correctly
  66. char *orig = setlocale(LC_NUMERIC, 0);
  67. setlocale(LC_NUMERIC, "C");
  68. #endif
  69. double res = strtod(string, &end);
  70. #if !defined(_WIN32_WCE) && !defined(ANDROID)
  71. // Restore the locale
  72. setlocale(LC_NUMERIC, orig);
  73. #endif
  74. if( numScanned )
  75. *numScanned = end - string;
  76. return res;
  77. }
  78. asQWORD asStringScanUInt64(const char *string, int base, size_t *numScanned)
  79. {
  80. asASSERT(base == 10 || base == 16);
  81. const char *end = string;
  82. asQWORD res = 0;
  83. if( base == 10 )
  84. {
  85. while( *end >= '0' && *end <= '9' )
  86. {
  87. res *= 10;
  88. res += *end++ - '0';
  89. }
  90. }
  91. else if( base == 16 )
  92. {
  93. while( (*end >= '0' && *end <= '9') ||
  94. (*end >= 'a' && *end <= 'f') ||
  95. (*end >= 'A' && *end <= 'F') )
  96. {
  97. res *= 16;
  98. if( *end >= '0' && *end <= '9' )
  99. res += *end++ - '0';
  100. else if( *end >= 'a' && *end <= 'f' )
  101. res += *end++ - 'a' + 10;
  102. else if( *end >= 'A' && *end <= 'F' )
  103. res += *end++ - 'A' + 10;
  104. }
  105. }
  106. if( numScanned )
  107. *numScanned = end - string;
  108. return res;
  109. }
  110. //
  111. // The function will encode the unicode code point into the outEncodedBuffer, and then
  112. // return the length of the encoded value. If the input value is not a valid unicode code
  113. // point, then the function will return -1.
  114. //
  115. // This function is taken from the AngelCode ToolBox.
  116. //
  117. int asStringEncodeUTF8(unsigned int value, char *outEncodedBuffer)
  118. {
  119. unsigned char *buf = (unsigned char*)outEncodedBuffer;
  120. int length = -1;
  121. if( value <= 0x7F )
  122. {
  123. buf[0] = static_cast<unsigned char>(value);
  124. return 1;
  125. }
  126. else if( value >= 0x80 && value <= 0x7FF )
  127. {
  128. // Encode it with 2 characters
  129. buf[0] = static_cast<unsigned char>(0xC0 + (value >> 6));
  130. length = 2;
  131. }
  132. else if( (value >= 0x800 && value <= 0xD7FF) || (value >= 0xE000 && value <= 0xFFFF) )
  133. {
  134. // Note: Values 0xD800 to 0xDFFF are not valid unicode characters
  135. buf[0] = static_cast<unsigned char>(0xE0 + (value >> 12));
  136. length = 3;
  137. }
  138. else if( value >= 0x10000 && value <= 0x10FFFF )
  139. {
  140. buf[0] = static_cast<unsigned char>(0xF0 + (value >> 18));
  141. length = 4;
  142. }
  143. int n = length-1;
  144. for( ; n > 0; n-- )
  145. {
  146. buf[n] = static_cast<unsigned char>(0x80 + (value & 0x3F));
  147. value >>= 6;
  148. }
  149. return length;
  150. }
  151. //
  152. // The function will decode an UTF8 character and return the unicode code point.
  153. // outLength will receive the number of bytes that were decoded.
  154. //
  155. // This function is taken from the AngelCode ToolBox.
  156. //
  157. int asStringDecodeUTF8(const char *encodedBuffer, unsigned int *outLength)
  158. {
  159. const unsigned char *buf = (const unsigned char*)encodedBuffer;
  160. int value = 0;
  161. int length = -1;
  162. unsigned char byte = buf[0];
  163. if( (byte & 0x80) == 0 )
  164. {
  165. // This is the only byte
  166. if( outLength ) *outLength = 1;
  167. return byte;
  168. }
  169. else if( (byte & 0xE0) == 0xC0 )
  170. {
  171. // There is one more byte
  172. value = int(byte & 0x1F);
  173. length = 2;
  174. // The value at this moment must not be less than 2, because
  175. // that should have been encoded with one byte only.
  176. if( value < 2 )
  177. length = -1;
  178. }
  179. else if( (byte & 0xF0) == 0xE0 )
  180. {
  181. // There are two more bytes
  182. value = int(byte & 0x0F);
  183. length = 3;
  184. }
  185. else if( (byte & 0xF8) == 0xF0 )
  186. {
  187. // There are three more bytes
  188. value = int(byte & 0x07);
  189. length = 4;
  190. }
  191. int n = 1;
  192. for( ; n < length; n++ )
  193. {
  194. byte = buf[n];
  195. if( (byte & 0xC0) == 0x80 )
  196. value = (value << 6) + int(byte & 0x3F);
  197. else
  198. break;
  199. }
  200. if( n == length )
  201. {
  202. if( outLength ) *outLength = (unsigned)length;
  203. return value;
  204. }
  205. // The byte sequence isn't a valid UTF-8 byte sequence.
  206. return -1;
  207. }
  208. //
  209. // The function will encode the unicode code point into the outEncodedBuffer, and then
  210. // return the length of the encoded value. If the input value is not a valid unicode code
  211. // point, then the function will return -1.
  212. //
  213. // This function is taken from the AngelCode ToolBox.
  214. //
  215. int asStringEncodeUTF16(unsigned int value, char *outEncodedBuffer)
  216. {
  217. if( value < 0x10000 )
  218. {
  219. #ifndef AS_BIG_ENDIAN
  220. outEncodedBuffer[0] = (value & 0xFF);
  221. outEncodedBuffer[1] = ((value >> 8) & 0xFF);
  222. #else
  223. outEncodedBuffer[1] = (value & 0xFF);
  224. outEncodedBuffer[0] = ((value >> 8) & 0xFF);
  225. #endif
  226. return 2;
  227. }
  228. else
  229. {
  230. value -= 0x10000;
  231. int surrogate1 = ((value >> 10) & 0x3FF) + 0xD800;
  232. int surrogate2 = (value & 0x3FF) + 0xDC00;
  233. #ifndef AS_BIG_ENDIAN
  234. outEncodedBuffer[0] = (surrogate1 & 0xFF);
  235. outEncodedBuffer[1] = ((surrogate1 >> 8) & 0xFF);
  236. outEncodedBuffer[2] = (surrogate2 & 0xFF);
  237. outEncodedBuffer[3] = ((surrogate2 >> 8) & 0xFF);
  238. #else
  239. outEncodedBuffer[1] = (surrogate1 & 0xFF);
  240. outEncodedBuffer[0] = ((surrogate1 >> 8) & 0xFF);
  241. outEncodedBuffer[3] = (surrogate2 & 0xFF);
  242. outEncodedBuffer[2] = ((surrogate2 >> 8) & 0xFF);
  243. #endif
  244. return 4;
  245. }
  246. }
  247. END_AS_NAMESPACE