as_string_util.cpp 9.0 KB

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