cstring.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1997-2011, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File CSTRING.C
  12. *
  13. * @author Helena Shih
  14. *
  15. * Modification History:
  16. *
  17. * Date Name Description
  18. * 6/18/98 hshih Created
  19. * 09/08/98 stephen Added include for ctype, for Mac Port
  20. * 11/15/99 helena Integrated S/390 IEEE changes.
  21. ******************************************************************************
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "unicode/utypes.h"
  26. #include "cmemory.h"
  27. #include "cstring.h"
  28. #include "uassert.h"
  29. /*
  30. * We hardcode case conversion for invariant characters to match our expectation
  31. * and the compiler execution charset.
  32. * This prevents problems on systems
  33. * - with non-default casing behavior, like Turkish system locales where
  34. * tolower('I') maps to dotless i and toupper('i') maps to dotted I
  35. * - where there are no lowercase Latin characters at all, or using different
  36. * codes (some old EBCDIC codepages)
  37. *
  38. * This works because the compiler usually runs on a platform where the execution
  39. * charset includes all of the invariant characters at their expected
  40. * code positions, so that the char * string literals in ICU code match
  41. * the char literals here.
  42. *
  43. * Note that the set of lowercase Latin letters is discontiguous in EBCDIC
  44. * and the set of uppercase Latin letters is discontiguous as well.
  45. */
  46. U_CAPI UBool U_EXPORT2
  47. uprv_isASCIILetter(char c) {
  48. #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
  49. return
  50. ('a'<=c && c<='i') || ('j'<=c && c<='r') || ('s'<=c && c<='z') ||
  51. ('A'<=c && c<='I') || ('J'<=c && c<='R') || ('S'<=c && c<='Z');
  52. #else
  53. return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
  54. #endif
  55. }
  56. U_CAPI char U_EXPORT2
  57. uprv_toupper(char c) {
  58. #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
  59. if(('a'<=c && c<='i') || ('j'<=c && c<='r') || ('s'<=c && c<='z')) {
  60. c=(char)(c+('A'-'a'));
  61. }
  62. #else
  63. if('a'<=c && c<='z') {
  64. c=(char)(c+('A'-'a'));
  65. }
  66. #endif
  67. return c;
  68. }
  69. #if 0
  70. /*
  71. * Commented out because cstring.h defines uprv_tolower() to be
  72. * the same as either uprv_asciitolower() or uprv_ebcdictolower()
  73. * to reduce the amount of code to cover with tests.
  74. *
  75. * Note that this uprv_tolower() definition is likely to work for most
  76. * charset families, not just ASCII and EBCDIC, because its #else branch
  77. * is written generically.
  78. */
  79. U_CAPI char U_EXPORT2
  80. uprv_tolower(char c) {
  81. #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
  82. if(('A'<=c && c<='I') || ('J'<=c && c<='R') || ('S'<=c && c<='Z')) {
  83. c=(char)(c+('a'-'A'));
  84. }
  85. #else
  86. if('A'<=c && c<='Z') {
  87. c=(char)(c+('a'-'A'));
  88. }
  89. #endif
  90. return c;
  91. }
  92. #endif
  93. U_CAPI char U_EXPORT2
  94. uprv_asciitolower(char c) {
  95. if(0x41<=c && c<=0x5a) {
  96. c=(char)(c+0x20);
  97. }
  98. return c;
  99. }
  100. U_CAPI char U_EXPORT2
  101. uprv_ebcdictolower(char c) {
  102. if( (0xc1<=(uint8_t)c && (uint8_t)c<=0xc9) ||
  103. (0xd1<=(uint8_t)c && (uint8_t)c<=0xd9) ||
  104. (0xe2<=(uint8_t)c && (uint8_t)c<=0xe9)
  105. ) {
  106. c=(char)(c-0x40);
  107. }
  108. return c;
  109. }
  110. U_CAPI char* U_EXPORT2
  111. T_CString_toLowerCase(char* str)
  112. {
  113. char* origPtr = str;
  114. if (str) {
  115. do
  116. *str = (char)uprv_tolower(*str);
  117. while (*(str++));
  118. }
  119. return origPtr;
  120. }
  121. U_CAPI char* U_EXPORT2
  122. T_CString_toUpperCase(char* str)
  123. {
  124. char* origPtr = str;
  125. if (str) {
  126. do
  127. *str = (char)uprv_toupper(*str);
  128. while (*(str++));
  129. }
  130. return origPtr;
  131. }
  132. /*
  133. * Takes a int32_t and fills in a char* string with that number "radix"-based.
  134. * Does not handle negative values (makes an empty string for them).
  135. * Writes at most 12 chars ("-2147483647" plus NUL).
  136. * Returns the length of the string (not including the NUL).
  137. */
  138. U_CAPI int32_t U_EXPORT2
  139. T_CString_integerToString(char* buffer, int32_t v, int32_t radix)
  140. {
  141. char tbuf[30];
  142. int32_t tbx = sizeof(tbuf);
  143. uint8_t digit;
  144. int32_t length = 0;
  145. uint32_t uval;
  146. U_ASSERT(radix>=2 && radix<=16);
  147. uval = (uint32_t) v;
  148. if(v<0 && radix == 10) {
  149. /* Only in base 10 do we conside numbers to be signed. */
  150. uval = (uint32_t)(-v);
  151. buffer[length++] = '-';
  152. }
  153. tbx = sizeof(tbuf)-1;
  154. tbuf[tbx] = 0; /* We are generating the digits backwards. Null term the end. */
  155. do {
  156. digit = (uint8_t)(uval % radix);
  157. tbuf[--tbx] = (char)(T_CString_itosOffset(digit));
  158. uval = uval / radix;
  159. } while (uval != 0);
  160. /* copy converted number into user buffer */
  161. uprv_strcpy(buffer+length, tbuf+tbx);
  162. length += sizeof(tbuf) - tbx -1;
  163. return length;
  164. }
  165. /*
  166. * Takes a int64_t and fills in a char* string with that number "radix"-based.
  167. * Writes at most 21: chars ("-9223372036854775807" plus NUL).
  168. * Returns the length of the string, not including the terminating NUL.
  169. */
  170. U_CAPI int32_t U_EXPORT2
  171. T_CString_int64ToString(char* buffer, int64_t v, uint32_t radix)
  172. {
  173. char tbuf[30];
  174. int32_t tbx = sizeof(tbuf);
  175. uint8_t digit;
  176. int32_t length = 0;
  177. uint64_t uval;
  178. U_ASSERT(radix>=2 && radix<=16);
  179. uval = (uint64_t) v;
  180. if(v<0 && radix == 10) {
  181. /* Only in base 10 do we conside numbers to be signed. */
  182. uval = (uint64_t)(-v);
  183. buffer[length++] = '-';
  184. }
  185. tbx = sizeof(tbuf)-1;
  186. tbuf[tbx] = 0; /* We are generating the digits backwards. Null term the end. */
  187. do {
  188. digit = (uint8_t)(uval % radix);
  189. tbuf[--tbx] = (char)(T_CString_itosOffset(digit));
  190. uval = uval / radix;
  191. } while (uval != 0);
  192. /* copy converted number into user buffer */
  193. uprv_strcpy(buffer+length, tbuf+tbx);
  194. length += sizeof(tbuf) - tbx -1;
  195. return length;
  196. }
  197. U_CAPI int32_t U_EXPORT2
  198. T_CString_stringToInteger(const char *integerString, int32_t radix)
  199. {
  200. char *end;
  201. return uprv_strtoul(integerString, &end, radix);
  202. }
  203. U_CAPI int U_EXPORT2
  204. uprv_stricmp(const char *str1, const char *str2) {
  205. if(str1==nullptr) {
  206. if(str2==nullptr) {
  207. return 0;
  208. } else {
  209. return -1;
  210. }
  211. } else if(str2==nullptr) {
  212. return 1;
  213. } else {
  214. /* compare non-nullptr strings lexically with lowercase */
  215. int rc;
  216. unsigned char c1, c2;
  217. for(;;) {
  218. c1=(unsigned char)*str1;
  219. c2=(unsigned char)*str2;
  220. if(c1==0) {
  221. if(c2==0) {
  222. return 0;
  223. } else {
  224. return -1;
  225. }
  226. } else if(c2==0) {
  227. return 1;
  228. } else {
  229. /* compare non-zero characters with lowercase */
  230. rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
  231. if(rc!=0) {
  232. return rc;
  233. }
  234. }
  235. ++str1;
  236. ++str2;
  237. }
  238. }
  239. }
  240. U_CAPI int U_EXPORT2
  241. uprv_strnicmp(const char *str1, const char *str2, uint32_t n) {
  242. if(str1==nullptr) {
  243. if(str2==nullptr) {
  244. return 0;
  245. } else {
  246. return -1;
  247. }
  248. } else if(str2==nullptr) {
  249. return 1;
  250. } else {
  251. /* compare non-nullptr strings lexically with lowercase */
  252. int rc;
  253. unsigned char c1, c2;
  254. for(; n--;) {
  255. c1=(unsigned char)*str1;
  256. c2=(unsigned char)*str2;
  257. if(c1==0) {
  258. if(c2==0) {
  259. return 0;
  260. } else {
  261. return -1;
  262. }
  263. } else if(c2==0) {
  264. return 1;
  265. } else {
  266. /* compare non-zero characters with lowercase */
  267. rc=(int)(unsigned char)uprv_tolower(c1)-(int)(unsigned char)uprv_tolower(c2);
  268. if(rc!=0) {
  269. return rc;
  270. }
  271. }
  272. ++str1;
  273. ++str2;
  274. }
  275. }
  276. return 0;
  277. }
  278. U_CAPI char* U_EXPORT2
  279. uprv_strdup(const char *src) {
  280. size_t len = uprv_strlen(src) + 1;
  281. char *dup = (char *) uprv_malloc(len);
  282. if (dup) {
  283. uprv_memcpy(dup, src, len);
  284. }
  285. return dup;
  286. }
  287. U_CAPI char* U_EXPORT2
  288. uprv_strndup(const char *src, int32_t n) {
  289. char *dup;
  290. if(n < 0) {
  291. dup = uprv_strdup(src);
  292. } else {
  293. dup = (char*)uprv_malloc(n+1);
  294. if (dup) {
  295. uprv_memcpy(dup, src, n);
  296. dup[n] = 0;
  297. }
  298. }
  299. return dup;
  300. }