unistr_cnv.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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) 1999-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: unistr_cnv.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:2
  14. *
  15. * created on: 2004aug19
  16. * created by: Markus W. Scherer
  17. *
  18. * Character conversion functions moved here from unistr.cpp
  19. */
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_CONVERSION
  22. #include "unicode/putil.h"
  23. #include "cstring.h"
  24. #include "cmemory.h"
  25. #include "unicode/ustring.h"
  26. #include "unicode/unistr.h"
  27. #include "unicode/ucnv.h"
  28. #include "ucnv_imp.h"
  29. #include "putilimp.h"
  30. #include "ustr_cnv.h"
  31. #include "ustr_imp.h"
  32. U_NAMESPACE_BEGIN
  33. //========================================
  34. // Constructors
  35. //========================================
  36. #if !U_CHARSET_IS_UTF8
  37. UnicodeString::UnicodeString(const char *codepageData) {
  38. fUnion.fFields.fLengthAndFlags = kShortString;
  39. if(codepageData != 0) {
  40. doCodepageCreate(codepageData, (int32_t)uprv_strlen(codepageData), 0);
  41. }
  42. }
  43. UnicodeString::UnicodeString(const char *codepageData,
  44. int32_t dataLength) {
  45. fUnion.fFields.fLengthAndFlags = kShortString;
  46. if(codepageData != 0) {
  47. doCodepageCreate(codepageData, dataLength, 0);
  48. }
  49. }
  50. // else see unistr.cpp
  51. #endif
  52. UnicodeString::UnicodeString(const char *codepageData,
  53. const char *codepage) {
  54. fUnion.fFields.fLengthAndFlags = kShortString;
  55. if (codepageData != nullptr) {
  56. doCodepageCreate(codepageData, static_cast<int32_t>(uprv_strlen(codepageData)), codepage);
  57. }
  58. }
  59. UnicodeString::UnicodeString(const char *codepageData,
  60. int32_t dataLength,
  61. const char *codepage) {
  62. fUnion.fFields.fLengthAndFlags = kShortString;
  63. if (codepageData != nullptr) {
  64. doCodepageCreate(codepageData, dataLength, codepage);
  65. }
  66. }
  67. UnicodeString::UnicodeString(const char *src, int32_t srcLength,
  68. UConverter *cnv,
  69. UErrorCode &errorCode) {
  70. fUnion.fFields.fLengthAndFlags = kShortString;
  71. if(U_SUCCESS(errorCode)) {
  72. // check arguments
  73. if(src==nullptr) {
  74. // treat as an empty string, do nothing more
  75. } else if(srcLength<-1) {
  76. errorCode=U_ILLEGAL_ARGUMENT_ERROR;
  77. } else {
  78. // get input length
  79. if(srcLength==-1) {
  80. srcLength = static_cast<int32_t>(uprv_strlen(src));
  81. }
  82. if(srcLength>0) {
  83. if (cnv != nullptr) {
  84. // use the provided converter
  85. ucnv_resetToUnicode(cnv);
  86. doCodepageCreate(src, srcLength, cnv, errorCode);
  87. } else {
  88. // use the default converter
  89. cnv=u_getDefaultConverter(&errorCode);
  90. doCodepageCreate(src, srcLength, cnv, errorCode);
  91. u_releaseDefaultConverter(cnv);
  92. }
  93. }
  94. }
  95. if(U_FAILURE(errorCode)) {
  96. setToBogus();
  97. }
  98. }
  99. }
  100. //========================================
  101. // Codeset conversion
  102. //========================================
  103. #if !U_CHARSET_IS_UTF8
  104. int32_t
  105. UnicodeString::extract(int32_t start,
  106. int32_t length,
  107. char *target,
  108. uint32_t dstSize) const {
  109. return extract(start, length, target, dstSize, 0);
  110. }
  111. // else see unistr.cpp
  112. #endif
  113. int32_t
  114. UnicodeString::extract(int32_t start,
  115. int32_t length,
  116. char *target,
  117. uint32_t dstSize,
  118. const char *codepage) const
  119. {
  120. // if the arguments are illegal, then do nothing
  121. if (/*dstSize < 0 || */(dstSize > 0 && target == nullptr)) {
  122. return 0;
  123. }
  124. // pin the indices to legal values
  125. pinIndices(start, length);
  126. // We need to cast dstSize to int32_t for all subsequent code.
  127. // I don't know why the API was defined with uint32_t but we are stuck with it.
  128. // Also, dstSize==0xffffffff means "unlimited" but if we use target+dstSize
  129. // as a limit in some functions, it may wrap around and yield a pointer
  130. // that compares less-than target.
  131. int32_t capacity;
  132. if(dstSize < 0x7fffffff) {
  133. // Assume that the capacity is real and a limit pointer won't wrap around.
  134. capacity = static_cast<int32_t>(dstSize);
  135. } else {
  136. // Pin the capacity so that a limit pointer does not wrap around.
  137. char* targetLimit = static_cast<char*>(U_MAX_PTR(target));
  138. // U_MAX_PTR(target) returns a targetLimit that is at most 0x7fffffff
  139. // greater than target and does not wrap around the top of the address space.
  140. capacity = static_cast<int32_t>(targetLimit - target);
  141. }
  142. // create the converter
  143. UConverter *converter;
  144. UErrorCode status = U_ZERO_ERROR;
  145. // just write the NUL if the string length is 0
  146. if(length == 0) {
  147. return u_terminateChars(target, capacity, 0, &status);
  148. }
  149. // if the codepage is the default, use our cache
  150. // if it is an empty string, then use the "invariant character" conversion
  151. if (codepage == nullptr) {
  152. const char *defaultName = ucnv_getDefaultName();
  153. if(UCNV_FAST_IS_UTF8(defaultName)) {
  154. return toUTF8(start, length, target, capacity);
  155. }
  156. converter = u_getDefaultConverter(&status);
  157. } else if (*codepage == 0) {
  158. // use the "invariant characters" conversion
  159. int32_t destLength;
  160. if(length <= capacity) {
  161. destLength = length;
  162. } else {
  163. destLength = capacity;
  164. }
  165. u_UCharsToChars(getArrayStart() + start, target, destLength);
  166. return u_terminateChars(target, capacity, length, &status);
  167. } else {
  168. converter = ucnv_open(codepage, &status);
  169. }
  170. length = doExtract(start, length, target, capacity, converter, status);
  171. // close the converter
  172. if (codepage == nullptr) {
  173. u_releaseDefaultConverter(converter);
  174. } else {
  175. ucnv_close(converter);
  176. }
  177. return length;
  178. }
  179. int32_t
  180. UnicodeString::extract(char *dest, int32_t destCapacity,
  181. UConverter *cnv,
  182. UErrorCode &errorCode) const
  183. {
  184. if(U_FAILURE(errorCode)) {
  185. return 0;
  186. }
  187. if (isBogus() || destCapacity < 0 || (destCapacity > 0 && dest == nullptr)) {
  188. errorCode=U_ILLEGAL_ARGUMENT_ERROR;
  189. return 0;
  190. }
  191. // nothing to do?
  192. if(isEmpty()) {
  193. return u_terminateChars(dest, destCapacity, 0, &errorCode);
  194. }
  195. // get the converter
  196. UBool isDefaultConverter;
  197. if (cnv == nullptr) {
  198. isDefaultConverter=true;
  199. cnv=u_getDefaultConverter(&errorCode);
  200. if(U_FAILURE(errorCode)) {
  201. return 0;
  202. }
  203. } else {
  204. isDefaultConverter=false;
  205. ucnv_resetFromUnicode(cnv);
  206. }
  207. // convert
  208. int32_t len=doExtract(0, length(), dest, destCapacity, cnv, errorCode);
  209. // release the converter
  210. if(isDefaultConverter) {
  211. u_releaseDefaultConverter(cnv);
  212. }
  213. return len;
  214. }
  215. int32_t
  216. UnicodeString::doExtract(int32_t start, int32_t length,
  217. char *dest, int32_t destCapacity,
  218. UConverter *cnv,
  219. UErrorCode &errorCode) const
  220. {
  221. if(U_FAILURE(errorCode)) {
  222. if(destCapacity!=0) {
  223. *dest=0;
  224. }
  225. return 0;
  226. }
  227. const char16_t *src=getArrayStart()+start, *srcLimit=src+length;
  228. char *originalDest=dest;
  229. const char *destLimit;
  230. if(destCapacity==0) {
  231. destLimit=dest=nullptr;
  232. } else if(destCapacity==-1) {
  233. // Pin the limit to U_MAX_PTR if the "magic" destCapacity is used.
  234. destLimit = static_cast<char*>(U_MAX_PTR(dest));
  235. // for NUL-termination, translate into highest int32_t
  236. destCapacity=0x7fffffff;
  237. } else {
  238. destLimit=dest+destCapacity;
  239. }
  240. // perform the conversion
  241. ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, &errorCode);
  242. length = static_cast<int32_t>(dest - originalDest);
  243. // if an overflow occurs, then get the preflighting length
  244. if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
  245. char buffer[1024];
  246. destLimit=buffer+sizeof(buffer);
  247. do {
  248. dest=buffer;
  249. errorCode=U_ZERO_ERROR;
  250. ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, &errorCode);
  251. length += static_cast<int32_t>(dest - buffer);
  252. } while(errorCode==U_BUFFER_OVERFLOW_ERROR);
  253. }
  254. return u_terminateChars(originalDest, destCapacity, length, &errorCode);
  255. }
  256. void
  257. UnicodeString::doCodepageCreate(const char *codepageData,
  258. int32_t dataLength,
  259. const char *codepage)
  260. {
  261. // if there's nothing to convert, do nothing
  262. if (codepageData == nullptr || dataLength == 0 || dataLength < -1) {
  263. return;
  264. }
  265. if(dataLength == -1) {
  266. dataLength = static_cast<int32_t>(uprv_strlen(codepageData));
  267. }
  268. UErrorCode status = U_ZERO_ERROR;
  269. // create the converter
  270. // if the codepage is the default, use our cache
  271. // if it is an empty string, then use the "invariant character" conversion
  272. UConverter *converter;
  273. if (codepage == nullptr) {
  274. const char *defaultName = ucnv_getDefaultName();
  275. if(UCNV_FAST_IS_UTF8(defaultName)) {
  276. setToUTF8(StringPiece(codepageData, dataLength));
  277. return;
  278. }
  279. converter = u_getDefaultConverter(&status);
  280. } else if (*codepage == 0) {
  281. // use the "invariant characters" conversion
  282. if(cloneArrayIfNeeded(dataLength, dataLength, false)) {
  283. u_charsToUChars(codepageData, getArrayStart(), dataLength);
  284. setLength(dataLength);
  285. } else {
  286. setToBogus();
  287. }
  288. return;
  289. } else {
  290. converter = ucnv_open(codepage, &status);
  291. }
  292. // if we failed, set the appropriate flags and return
  293. if(U_FAILURE(status)) {
  294. setToBogus();
  295. return;
  296. }
  297. // perform the conversion
  298. doCodepageCreate(codepageData, dataLength, converter, status);
  299. if(U_FAILURE(status)) {
  300. setToBogus();
  301. }
  302. // close the converter
  303. if (codepage == nullptr) {
  304. u_releaseDefaultConverter(converter);
  305. } else {
  306. ucnv_close(converter);
  307. }
  308. }
  309. void
  310. UnicodeString::doCodepageCreate(const char *codepageData,
  311. int32_t dataLength,
  312. UConverter *converter,
  313. UErrorCode &status)
  314. {
  315. if(U_FAILURE(status)) {
  316. return;
  317. }
  318. // set up the conversion parameters
  319. const char *mySource = codepageData;
  320. const char *mySourceEnd = mySource + dataLength;
  321. char16_t *array, *myTarget;
  322. // estimate the size needed:
  323. int32_t arraySize;
  324. if(dataLength <= US_STACKBUF_SIZE) {
  325. // try to use the stack buffer
  326. arraySize = US_STACKBUF_SIZE;
  327. } else {
  328. // 1.25 char16_t's per source byte should cover most cases
  329. arraySize = dataLength + (dataLength >> 2);
  330. }
  331. // we do not care about the current contents
  332. UBool doCopyArray = false;
  333. for(;;) {
  334. if(!cloneArrayIfNeeded(arraySize, arraySize, doCopyArray)) {
  335. setToBogus();
  336. break;
  337. }
  338. // perform the conversion
  339. array = getArrayStart();
  340. myTarget = array + length();
  341. ucnv_toUnicode(converter, &myTarget, array + getCapacity(),
  342. &mySource, mySourceEnd, nullptr, true, &status);
  343. // update the conversion parameters
  344. setLength(static_cast<int32_t>(myTarget - array));
  345. // allocate more space and copy data, if needed
  346. if(status == U_BUFFER_OVERFLOW_ERROR) {
  347. // reset the error code
  348. status = U_ZERO_ERROR;
  349. // keep the previous conversion results
  350. doCopyArray = true;
  351. // estimate the new size needed, larger than before
  352. // try 2 char16_t's per remaining source byte
  353. arraySize = static_cast<int32_t>(length() + 2 * (mySourceEnd - mySource));
  354. } else {
  355. break;
  356. }
  357. }
  358. }
  359. U_NAMESPACE_END
  360. #endif