ustrenum.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2002-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Author: Alan Liu
  9. * Created: November 11 2002
  10. * Since: ICU 2.4
  11. **********************************************************************
  12. */
  13. #include "utypeinfo.h" // for 'typeid' to work
  14. #include "unicode/ustring.h"
  15. #include "unicode/strenum.h"
  16. #include "unicode/putil.h"
  17. #include "uenumimp.h"
  18. #include "ustrenum.h"
  19. #include "cstring.h"
  20. #include "cmemory.h"
  21. #include "uassert.h"
  22. U_NAMESPACE_BEGIN
  23. // StringEnumeration implementation ---------------------------------------- ***
  24. StringEnumeration::StringEnumeration()
  25. : chars(charsBuffer), charsCapacity(sizeof(charsBuffer)) {
  26. }
  27. StringEnumeration::~StringEnumeration() {
  28. if (chars != nullptr && chars != charsBuffer) {
  29. uprv_free(chars);
  30. }
  31. }
  32. // StringEnumeration base class clone() default implementation, does not clone
  33. StringEnumeration *
  34. StringEnumeration::clone() const {
  35. return nullptr;
  36. }
  37. const char *
  38. StringEnumeration::next(int32_t *resultLength, UErrorCode &status) {
  39. const UnicodeString *s=snext(status);
  40. if(U_SUCCESS(status) && s!=nullptr) {
  41. unistr=*s;
  42. ensureCharsCapacity(unistr.length()+1, status);
  43. if(U_SUCCESS(status)) {
  44. if(resultLength!=nullptr) {
  45. *resultLength=unistr.length();
  46. }
  47. unistr.extract(0, INT32_MAX, chars, charsCapacity, US_INV);
  48. return chars;
  49. }
  50. }
  51. return nullptr;
  52. }
  53. const char16_t *
  54. StringEnumeration::unext(int32_t *resultLength, UErrorCode &status) {
  55. const UnicodeString *s=snext(status);
  56. if(U_SUCCESS(status) && s!=nullptr) {
  57. unistr=*s;
  58. if(resultLength!=nullptr) {
  59. *resultLength=unistr.length();
  60. }
  61. return unistr.getTerminatedBuffer();
  62. }
  63. return nullptr;
  64. }
  65. const UnicodeString *
  66. StringEnumeration::snext(UErrorCode &status) {
  67. int32_t length;
  68. const char *s=next(&length, status);
  69. return setChars(s, length, status);
  70. }
  71. void
  72. StringEnumeration::ensureCharsCapacity(int32_t capacity, UErrorCode &status) {
  73. if(U_SUCCESS(status) && capacity>charsCapacity) {
  74. if(capacity<(charsCapacity+charsCapacity/2)) {
  75. // avoid allocation thrashing
  76. capacity=charsCapacity+charsCapacity/2;
  77. }
  78. if(chars!=charsBuffer) {
  79. uprv_free(chars);
  80. }
  81. chars = static_cast<char*>(uprv_malloc(capacity));
  82. if(chars==nullptr) {
  83. chars=charsBuffer;
  84. charsCapacity=sizeof(charsBuffer);
  85. status=U_MEMORY_ALLOCATION_ERROR;
  86. } else {
  87. charsCapacity=capacity;
  88. }
  89. }
  90. }
  91. UnicodeString *
  92. StringEnumeration::setChars(const char *s, int32_t length, UErrorCode &status) {
  93. if(U_SUCCESS(status) && s!=nullptr) {
  94. if(length<0) {
  95. length = static_cast<int32_t>(uprv_strlen(s));
  96. }
  97. char16_t *buffer=unistr.getBuffer(length+1);
  98. if(buffer!=nullptr) {
  99. u_charsToUChars(s, buffer, length);
  100. buffer[length]=0;
  101. unistr.releaseBuffer(length);
  102. return &unistr;
  103. } else {
  104. status=U_MEMORY_ALLOCATION_ERROR;
  105. }
  106. }
  107. return nullptr;
  108. }
  109. bool
  110. StringEnumeration::operator==(const StringEnumeration& that)const {
  111. return typeid(*this) == typeid(that);
  112. }
  113. bool
  114. StringEnumeration::operator!=(const StringEnumeration& that)const {
  115. return !operator==(that);
  116. }
  117. // UStringEnumeration implementation --------------------------------------- ***
  118. UStringEnumeration * U_EXPORT2
  119. UStringEnumeration::fromUEnumeration(
  120. UEnumeration *uenumToAdopt, UErrorCode &status) {
  121. if (U_FAILURE(status)) {
  122. uenum_close(uenumToAdopt);
  123. return nullptr;
  124. }
  125. UStringEnumeration *result = new UStringEnumeration(uenumToAdopt);
  126. if (result == nullptr) {
  127. status = U_MEMORY_ALLOCATION_ERROR;
  128. uenum_close(uenumToAdopt);
  129. return nullptr;
  130. }
  131. return result;
  132. }
  133. UStringEnumeration::UStringEnumeration(UEnumeration* _uenum) :
  134. uenum(_uenum) {
  135. U_ASSERT(_uenum != 0);
  136. }
  137. UStringEnumeration::~UStringEnumeration() {
  138. uenum_close(uenum);
  139. }
  140. int32_t UStringEnumeration::count(UErrorCode& status) const {
  141. return uenum_count(uenum, &status);
  142. }
  143. const char *UStringEnumeration::next(int32_t *resultLength, UErrorCode &status) {
  144. return uenum_next(uenum, resultLength, &status);
  145. }
  146. const UnicodeString* UStringEnumeration::snext(UErrorCode& status) {
  147. int32_t length;
  148. const char16_t* str = uenum_unext(uenum, &length, &status);
  149. if (str == nullptr || U_FAILURE(status)) {
  150. return nullptr;
  151. }
  152. return &unistr.setTo(str, length);
  153. }
  154. void UStringEnumeration::reset(UErrorCode& status) {
  155. uenum_reset(uenum, &status);
  156. }
  157. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration)
  158. U_NAMESPACE_END
  159. // C wrapper --------------------------------------------------------------- ***
  160. #define THIS(en) ((icu::StringEnumeration*)(en->context))
  161. U_CDECL_BEGIN
  162. /**
  163. * Wrapper API to make StringEnumeration look like UEnumeration.
  164. */
  165. static void U_CALLCONV
  166. ustrenum_close(UEnumeration* en) {
  167. delete THIS(en);
  168. uprv_free(en);
  169. }
  170. /**
  171. * Wrapper API to make StringEnumeration look like UEnumeration.
  172. */
  173. static int32_t U_CALLCONV
  174. ustrenum_count(UEnumeration* en,
  175. UErrorCode* ec)
  176. {
  177. return THIS(en)->count(*ec);
  178. }
  179. /**
  180. * Wrapper API to make StringEnumeration look like UEnumeration.
  181. */
  182. static const char16_t* U_CALLCONV
  183. ustrenum_unext(UEnumeration* en,
  184. int32_t* resultLength,
  185. UErrorCode* ec)
  186. {
  187. return THIS(en)->unext(resultLength, *ec);
  188. }
  189. /**
  190. * Wrapper API to make StringEnumeration look like UEnumeration.
  191. */
  192. static const char* U_CALLCONV
  193. ustrenum_next(UEnumeration* en,
  194. int32_t* resultLength,
  195. UErrorCode* ec)
  196. {
  197. return THIS(en)->next(resultLength, *ec);
  198. }
  199. /**
  200. * Wrapper API to make StringEnumeration look like UEnumeration.
  201. */
  202. static void U_CALLCONV
  203. ustrenum_reset(UEnumeration* en,
  204. UErrorCode* ec)
  205. {
  206. THIS(en)->reset(*ec);
  207. }
  208. /**
  209. * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
  210. * The StringEnumeration pointer will be stored in 'context'.
  211. */
  212. static const UEnumeration USTRENUM_VT = {
  213. nullptr,
  214. nullptr, // store StringEnumeration pointer here
  215. ustrenum_close,
  216. ustrenum_count,
  217. ustrenum_unext,
  218. ustrenum_next,
  219. ustrenum_reset
  220. };
  221. U_CDECL_END
  222. /**
  223. * Given a StringEnumeration, wrap it in a UEnumeration. The
  224. * StringEnumeration is adopted; after this call, the caller must not
  225. * delete it (regardless of error status).
  226. */
  227. U_CAPI UEnumeration* U_EXPORT2
  228. uenum_openFromStringEnumeration(icu::StringEnumeration* adopted, UErrorCode* ec) {
  229. UEnumeration* result = nullptr;
  230. if (U_SUCCESS(*ec) && adopted != nullptr) {
  231. result = (UEnumeration*) uprv_malloc(sizeof(UEnumeration));
  232. if (result == nullptr) {
  233. *ec = U_MEMORY_ALLOCATION_ERROR;
  234. } else {
  235. uprv_memcpy(result, &USTRENUM_VT, sizeof(USTRENUM_VT));
  236. result->context = adopted;
  237. }
  238. }
  239. if (result == nullptr) {
  240. delete adopted;
  241. }
  242. return result;
  243. }
  244. // C wrapper --------------------------------------------------------------- ***
  245. U_CDECL_BEGIN
  246. typedef struct UCharStringEnumeration {
  247. UEnumeration uenum;
  248. int32_t index, count;
  249. } UCharStringEnumeration;
  250. static void U_CALLCONV
  251. ucharstrenum_close(UEnumeration* en) {
  252. uprv_free(en);
  253. }
  254. static int32_t U_CALLCONV
  255. ucharstrenum_count(UEnumeration* en,
  256. UErrorCode* /*ec*/) {
  257. return ((UCharStringEnumeration*)en)->count;
  258. }
  259. static const char16_t* U_CALLCONV
  260. ucharstrenum_unext(UEnumeration* en,
  261. int32_t* resultLength,
  262. UErrorCode* /*ec*/) {
  263. UCharStringEnumeration *e = (UCharStringEnumeration*) en;
  264. if (e->index >= e->count) {
  265. return nullptr;
  266. }
  267. const char16_t* result = ((const char16_t**)e->uenum.context)[e->index++];
  268. if (resultLength) {
  269. *resultLength = u_strlen(result);
  270. }
  271. return result;
  272. }
  273. static const char* U_CALLCONV
  274. ucharstrenum_next(UEnumeration* en,
  275. int32_t* resultLength,
  276. UErrorCode* /*ec*/) {
  277. UCharStringEnumeration *e = (UCharStringEnumeration*) en;
  278. if (e->index >= e->count) {
  279. return nullptr;
  280. }
  281. const char* result = ((const char**)e->uenum.context)[e->index++];
  282. if (resultLength) {
  283. *resultLength = (int32_t)uprv_strlen(result);
  284. }
  285. return result;
  286. }
  287. static void U_CALLCONV
  288. ucharstrenum_reset(UEnumeration* en,
  289. UErrorCode* /*ec*/) {
  290. ((UCharStringEnumeration*)en)->index = 0;
  291. }
  292. static const UEnumeration UCHARSTRENUM_VT = {
  293. nullptr,
  294. nullptr, // store StringEnumeration pointer here
  295. ucharstrenum_close,
  296. ucharstrenum_count,
  297. uenum_unextDefault,
  298. ucharstrenum_next,
  299. ucharstrenum_reset
  300. };
  301. static const UEnumeration UCHARSTRENUM_U_VT = {
  302. nullptr,
  303. nullptr, // store StringEnumeration pointer here
  304. ucharstrenum_close,
  305. ucharstrenum_count,
  306. ucharstrenum_unext,
  307. uenum_nextDefault,
  308. ucharstrenum_reset
  309. };
  310. U_CDECL_END
  311. U_CAPI UEnumeration* U_EXPORT2
  312. uenum_openCharStringsEnumeration(const char* const strings[], int32_t count,
  313. UErrorCode* ec) {
  314. UCharStringEnumeration* result = nullptr;
  315. if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != nullptr)) {
  316. result = (UCharStringEnumeration*) uprv_malloc(sizeof(UCharStringEnumeration));
  317. if (result == nullptr) {
  318. *ec = U_MEMORY_ALLOCATION_ERROR;
  319. } else {
  320. U_ASSERT((char*)result==(char*)(&result->uenum));
  321. uprv_memcpy(result, &UCHARSTRENUM_VT, sizeof(UCHARSTRENUM_VT));
  322. result->uenum.context = (void*)strings;
  323. result->index = 0;
  324. result->count = count;
  325. }
  326. }
  327. return (UEnumeration*) result;
  328. }
  329. U_CAPI UEnumeration* U_EXPORT2
  330. uenum_openUCharStringsEnumeration(const char16_t* const strings[], int32_t count,
  331. UErrorCode* ec) {
  332. UCharStringEnumeration* result = nullptr;
  333. if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != nullptr)) {
  334. result = (UCharStringEnumeration*) uprv_malloc(sizeof(UCharStringEnumeration));
  335. if (result == nullptr) {
  336. *ec = U_MEMORY_ALLOCATION_ERROR;
  337. } else {
  338. U_ASSERT((char*)result==(char*)(&result->uenum));
  339. uprv_memcpy(result, &UCHARSTRENUM_U_VT, sizeof(UCHARSTRENUM_U_VT));
  340. result->uenum.context = (void*)strings;
  341. result->index = 0;
  342. result->count = count;
  343. }
  344. }
  345. return (UEnumeration*) result;
  346. }
  347. // end C Wrapper