as_string_util.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2016 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. // Modified by Lasse Oorni for Urho3D
  24. #include "as_config.h"
  25. #include <string.h> // some compilers declare memcpy() here
  26. #include <math.h> // pow()
  27. // Urho3D: for compiler reliability, define ourselves
  28. //#include <stdint.h> // UINT64_MAX
  29. #define UINT64_MAX 0xffffffffffffffffULL
  30. #if !defined(AS_NO_MEMORY_H)
  31. #include <memory.h>
  32. #endif
  33. #include "as_string.h"
  34. #include "as_string_util.h"
  35. BEGIN_AS_NAMESPACE
  36. int asCompareStrings(const char *str1, size_t len1, const char *str2, size_t len2)
  37. {
  38. if( len1 == 0 )
  39. {
  40. if( str2 == 0 || len2 == 0 ) return 0; // Equal
  41. return 1; // The other string is larger than this
  42. }
  43. if( str2 == 0 )
  44. {
  45. if( len1 == 0 )
  46. return 0; // Equal
  47. return -1; // The other string is smaller than this
  48. }
  49. if( len2 < len1 )
  50. {
  51. int result = memcmp(str1, str2, len2);
  52. if( result == 0 ) return -1; // The other string is smaller than this
  53. return result;
  54. }
  55. int result = memcmp(str1, str2, len1);
  56. if( result == 0 && len1 < len2 ) return 1; // The other string is larger than this
  57. return result;
  58. }
  59. double asStringScanDouble(const char *string, size_t *numScanned)
  60. {
  61. // I decided to do my own implementation of strtod() because this function
  62. // doesn't seem to be present on all systems. iOS 5 for example doesn't appear
  63. // to include the function in the standard lib.
  64. // Another reason is that the standard implementation of strtod() is dependent
  65. // on the locale on some systems, i.e. it may use comma instead of dot for
  66. // the decimal indicator. This can be avoided by forcing the locale to "C" with
  67. // setlocale(), but this is another thing that is highly platform dependent.
  68. double value = 0;
  69. double fraction = 0.1;
  70. int exponent = 0;
  71. bool negativeExponent = false;
  72. int c = 0;
  73. // The tokenizer separates the sign from the number in
  74. // two tokens so we'll never have a sign to parse here
  75. // Parse the integer value
  76. for( ;; )
  77. {
  78. if( string[c] >= '0' && string[c] <= '9' )
  79. value = value*10 + double(string[c] - '0');
  80. else
  81. break;
  82. c++;
  83. }
  84. if( string[c] == '.' )
  85. {
  86. c++;
  87. // Parse the fraction
  88. for( ;; )
  89. {
  90. if( string[c] >= '0' && string[c] <= '9' )
  91. value += fraction * double(string[c] - '0');
  92. else
  93. break;
  94. c++;
  95. fraction *= 0.1;
  96. }
  97. }
  98. if( string[c] == 'e' || string[c] == 'E' )
  99. {
  100. c++;
  101. // Parse the sign of the exponent
  102. if( string[c] == '-' )
  103. {
  104. negativeExponent = true;
  105. c++;
  106. }
  107. else if( string[c] == '+' )
  108. c++;
  109. // Parse the exponent value
  110. for( ;; )
  111. {
  112. if( string[c] >= '0' && string[c] <= '9' )
  113. exponent = exponent*10 + int(string[c] - '0');
  114. else
  115. break;
  116. c++;
  117. }
  118. }
  119. if( exponent )
  120. {
  121. if( negativeExponent )
  122. exponent = -exponent;
  123. value *= pow(10.0, exponent);
  124. }
  125. if( numScanned )
  126. *numScanned = c;
  127. return value;
  128. }
  129. // Converts a character to the decimal number based on the radix
  130. // Returns -1 if the character is not valid for the radix
  131. static int asCharToNbr(char ch, int radix)
  132. {
  133. if( ch >= '0' && ch <= '9' ) return ((ch -= '0') < radix ? ch : -1);
  134. if( ch >= 'A' && ch <= 'Z' ) return ((ch -= 'A'-10) < radix ? ch : -1);
  135. if( ch >= 'a' && ch <= 'z' ) return ((ch -= 'a'-10) < radix ? ch : -1);
  136. return -1;
  137. }
  138. // If base is 0 the string should be prefixed by 0x, 0d, 0o, or 0b to allow the function to automatically determine the radix
  139. asQWORD asStringScanUInt64(const char *string, int base, size_t *numScanned, bool *overflow)
  140. {
  141. asASSERT(base == 10 || base == 16 || base == 0);
  142. if (overflow)
  143. *overflow = false;
  144. const char *end = string;
  145. asQWORD res = 0;
  146. if( base == 10 )
  147. {
  148. while( *end >= '0' && *end <= '9' )
  149. {
  150. if( overflow && ((res > UINT64_MAX / 10) || ((asUINT(*end - '0') > (UINT64_MAX - (UINT64_MAX / 10) * 10)) && res == UINT64_MAX / 10)) )
  151. *overflow = true;
  152. res *= 10;
  153. res += *end++ - '0';
  154. }
  155. }
  156. else
  157. {
  158. if( base == 0 && string[0] == '0')
  159. {
  160. // Determine the radix from the prefix
  161. switch( string[1] )
  162. {
  163. case 'b': case 'B': base = 2; break;
  164. case 'o': case 'O': base = 8; break;
  165. case 'd': case 'D': base = 10; break;
  166. case 'x': case 'X': base = 16; break;
  167. }
  168. end += 2;
  169. }
  170. asASSERT( base );
  171. if( base )
  172. {
  173. for (int nbr; (nbr = asCharToNbr(*end, base)) >= 0; end++)
  174. {
  175. if (overflow && ((res > UINT64_MAX / base) || ((asUINT(nbr) > (UINT64_MAX - (UINT64_MAX / base) * base)) && res == UINT64_MAX / base)) )
  176. *overflow = true;
  177. res = res * base + nbr;
  178. }
  179. }
  180. }
  181. if( numScanned )
  182. *numScanned = end - string;
  183. return res;
  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 asStringEncodeUTF8(unsigned int value, char *outEncodedBuffer)
  193. {
  194. unsigned char *buf = (unsigned char*)outEncodedBuffer;
  195. int length = -1;
  196. if( value <= 0x7F )
  197. {
  198. buf[0] = static_cast<unsigned char>(value);
  199. return 1;
  200. }
  201. else if( value >= 0x80 && value <= 0x7FF )
  202. {
  203. // Encode it with 2 characters
  204. buf[0] = static_cast<unsigned char>(0xC0 + (value >> 6));
  205. length = 2;
  206. }
  207. else if( (value >= 0x800 && value <= 0xD7FF) || (value >= 0xE000 && value <= 0xFFFF) )
  208. {
  209. // Note: Values 0xD800 to 0xDFFF are not valid unicode characters
  210. buf[0] = static_cast<unsigned char>(0xE0 + (value >> 12));
  211. length = 3;
  212. }
  213. else if( value >= 0x10000 && value <= 0x10FFFF )
  214. {
  215. buf[0] = static_cast<unsigned char>(0xF0 + (value >> 18));
  216. length = 4;
  217. }
  218. int n = length-1;
  219. for( ; n > 0; n-- )
  220. {
  221. buf[n] = static_cast<unsigned char>(0x80 + (value & 0x3F));
  222. value >>= 6;
  223. }
  224. return length;
  225. }
  226. //
  227. // The function will decode an UTF8 character and return the unicode code point.
  228. // outLength will receive the number of bytes that were decoded.
  229. //
  230. // This function is taken from the AngelCode ToolBox.
  231. //
  232. int asStringDecodeUTF8(const char *encodedBuffer, unsigned int *outLength)
  233. {
  234. const unsigned char *buf = (const unsigned char*)encodedBuffer;
  235. int value = 0;
  236. int length = -1;
  237. unsigned char byte = buf[0];
  238. if( (byte & 0x80) == 0 )
  239. {
  240. // This is the only byte
  241. if( outLength ) *outLength = 1;
  242. return byte;
  243. }
  244. else if( (byte & 0xE0) == 0xC0 )
  245. {
  246. // There is one more byte
  247. value = int(byte & 0x1F);
  248. length = 2;
  249. // The value at this moment must not be less than 2, because
  250. // that should have been encoded with one byte only.
  251. if( value < 2 )
  252. length = -1;
  253. }
  254. else if( (byte & 0xF0) == 0xE0 )
  255. {
  256. // There are two more bytes
  257. value = int(byte & 0x0F);
  258. length = 3;
  259. }
  260. else if( (byte & 0xF8) == 0xF0 )
  261. {
  262. // There are three more bytes
  263. value = int(byte & 0x07);
  264. length = 4;
  265. }
  266. int n = 1;
  267. for( ; n < length; n++ )
  268. {
  269. byte = buf[n];
  270. if( (byte & 0xC0) == 0x80 )
  271. value = (value << 6) + int(byte & 0x3F);
  272. else
  273. break;
  274. }
  275. if( n == length )
  276. {
  277. if( outLength ) *outLength = (unsigned)length;
  278. return value;
  279. }
  280. // The byte sequence isn't a valid UTF-8 byte sequence.
  281. return -1;
  282. }
  283. //
  284. // The function will encode the unicode code point into the outEncodedBuffer, and then
  285. // return the length of the encoded value. If the input value is not a valid unicode code
  286. // point, then the function will return -1.
  287. //
  288. // This function is taken from the AngelCode ToolBox.
  289. //
  290. int asStringEncodeUTF16(unsigned int value, char *outEncodedBuffer)
  291. {
  292. if( value < 0x10000 )
  293. {
  294. #ifndef AS_BIG_ENDIAN
  295. outEncodedBuffer[0] = (value & 0xFF);
  296. outEncodedBuffer[1] = ((value >> 8) & 0xFF);
  297. #else
  298. outEncodedBuffer[1] = (value & 0xFF);
  299. outEncodedBuffer[0] = ((value >> 8) & 0xFF);
  300. #endif
  301. return 2;
  302. }
  303. else
  304. {
  305. value -= 0x10000;
  306. int surrogate1 = ((value >> 10) & 0x3FF) + 0xD800;
  307. int surrogate2 = (value & 0x3FF) + 0xDC00;
  308. #ifndef AS_BIG_ENDIAN
  309. outEncodedBuffer[0] = (surrogate1 & 0xFF);
  310. outEncodedBuffer[1] = ((surrogate1 >> 8) & 0xFF);
  311. outEncodedBuffer[2] = (surrogate2 & 0xFF);
  312. outEncodedBuffer[3] = ((surrogate2 >> 8) & 0xFF);
  313. #else
  314. outEncodedBuffer[1] = (surrogate1 & 0xFF);
  315. outEncodedBuffer[0] = ((surrogate1 >> 8) & 0xFF);
  316. outEncodedBuffer[3] = (surrogate2 & 0xFF);
  317. outEncodedBuffer[2] = ((surrogate2 >> 8) & 0xFF);
  318. #endif
  319. return 4;
  320. }
  321. }
  322. END_AS_NAMESPACE