ucnv_cnv.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-2004, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * uconv_cnv.c:
  12. * Implements all the low level conversion functions
  13. * T_UnicodeConverter_{to,from}Unicode_$ConversionType
  14. *
  15. * Change history:
  16. *
  17. * 06/29/2000 helena Major rewrite of the callback APIs.
  18. */
  19. #include "unicode/utypes.h"
  20. #if !UCONFIG_NO_CONVERSION
  21. #include "unicode/ucnv_err.h"
  22. #include "unicode/ucnv.h"
  23. #include "unicode/uset.h"
  24. #include "ucnv_cnv.h"
  25. #include "ucnv_bld.h"
  26. #include "cmemory.h"
  27. U_CFUNC void
  28. ucnv_getCompleteUnicodeSet(const UConverter *cnv,
  29. const USetAdder *sa,
  30. UConverterUnicodeSet which,
  31. UErrorCode *pErrorCode) {
  32. (void)cnv;
  33. (void)which;
  34. (void)pErrorCode;
  35. sa->addRange(sa->set, 0, 0x10ffff);
  36. }
  37. U_CFUNC void
  38. ucnv_getNonSurrogateUnicodeSet(const UConverter *cnv,
  39. const USetAdder *sa,
  40. UConverterUnicodeSet which,
  41. UErrorCode *pErrorCode) {
  42. (void)cnv;
  43. (void)which;
  44. (void)pErrorCode;
  45. sa->addRange(sa->set, 0, 0xd7ff);
  46. sa->addRange(sa->set, 0xe000, 0x10ffff);
  47. }
  48. U_CFUNC void
  49. ucnv_fromUWriteBytes(UConverter *cnv,
  50. const char *bytes, int32_t length,
  51. char **target, const char *targetLimit,
  52. int32_t **offsets,
  53. int32_t sourceIndex,
  54. UErrorCode *pErrorCode) {
  55. char *t=*target;
  56. int32_t *o;
  57. /* write bytes */
  58. if(offsets==nullptr || (o=*offsets)==nullptr) {
  59. while(length>0 && t<targetLimit) {
  60. *t++=*bytes++;
  61. --length;
  62. }
  63. } else {
  64. /* output with offsets */
  65. while(length>0 && t<targetLimit) {
  66. *t++=*bytes++;
  67. *o++=sourceIndex;
  68. --length;
  69. }
  70. *offsets=o;
  71. }
  72. *target=t;
  73. /* write overflow */
  74. if(length>0) {
  75. if(cnv!=nullptr) {
  76. t=(char *)cnv->charErrorBuffer;
  77. cnv->charErrorBufferLength=(int8_t)length;
  78. do {
  79. *t++=(uint8_t)*bytes++;
  80. } while(--length>0);
  81. }
  82. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  83. }
  84. }
  85. U_CFUNC void
  86. ucnv_toUWriteUChars(UConverter *cnv,
  87. const char16_t *uchars, int32_t length,
  88. char16_t **target, const char16_t *targetLimit,
  89. int32_t **offsets,
  90. int32_t sourceIndex,
  91. UErrorCode *pErrorCode) {
  92. char16_t *t=*target;
  93. int32_t *o;
  94. /* write UChars */
  95. if(offsets==nullptr || (o=*offsets)==nullptr) {
  96. while(length>0 && t<targetLimit) {
  97. *t++=*uchars++;
  98. --length;
  99. }
  100. } else {
  101. /* output with offsets */
  102. while(length>0 && t<targetLimit) {
  103. *t++=*uchars++;
  104. *o++=sourceIndex;
  105. --length;
  106. }
  107. *offsets=o;
  108. }
  109. *target=t;
  110. /* write overflow */
  111. if(length>0) {
  112. if(cnv!=nullptr) {
  113. t=cnv->UCharErrorBuffer;
  114. cnv->UCharErrorBufferLength=(int8_t)length;
  115. do {
  116. *t++=*uchars++;
  117. } while(--length>0);
  118. }
  119. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  120. }
  121. }
  122. U_CFUNC void
  123. ucnv_toUWriteCodePoint(UConverter *cnv,
  124. UChar32 c,
  125. char16_t **target, const char16_t *targetLimit,
  126. int32_t **offsets,
  127. int32_t sourceIndex,
  128. UErrorCode *pErrorCode) {
  129. char16_t *t;
  130. int32_t *o;
  131. t=*target;
  132. if(t<targetLimit) {
  133. if(c<=0xffff) {
  134. *t++=(char16_t)c;
  135. c=U_SENTINEL;
  136. } else /* c is a supplementary code point */ {
  137. *t++=U16_LEAD(c);
  138. c=U16_TRAIL(c);
  139. if(t<targetLimit) {
  140. *t++=(char16_t)c;
  141. c=U_SENTINEL;
  142. }
  143. }
  144. /* write offsets */
  145. if(offsets!=nullptr && (o=*offsets)!=nullptr) {
  146. *o++=sourceIndex;
  147. if((*target+1)<t) {
  148. *o++=sourceIndex;
  149. }
  150. *offsets=o;
  151. }
  152. }
  153. *target=t;
  154. /* write overflow from c */
  155. if(c>=0) {
  156. if(cnv!=nullptr) {
  157. int8_t i=0;
  158. U16_APPEND_UNSAFE(cnv->UCharErrorBuffer, i, c);
  159. cnv->UCharErrorBufferLength=i;
  160. }
  161. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  162. }
  163. }
  164. #endif