utf_old.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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) 2002-2012, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: utf_old.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2002sep21
  16. * created by: Markus W. Scherer
  17. */
  18. /**
  19. * \file
  20. * \brief C API: Deprecated macros for Unicode string handling
  21. *
  22. * The macros in utf_old.h are all deprecated and their use discouraged.
  23. * Some of the design principles behind the set of UTF macros
  24. * have changed or proved impractical.
  25. * Almost all of the old "UTF macros" are at least renamed.
  26. * If you are looking for a new equivalent to an old macro, please see the
  27. * comment at the old one.
  28. *
  29. * Brief summary of reasons for deprecation:
  30. * - Switch on UTF_SIZE (selection of UTF-8/16/32 default string processing)
  31. * was impractical.
  32. * - Switch on UTF_SAFE etc. (selection of unsafe/safe/strict default string processing)
  33. * was of little use and impractical.
  34. * - Whole classes of macros became obsolete outside of the UTF_SIZE/UTF_SAFE
  35. * selection framework: UTF32_ macros (all trivial)
  36. * and UTF_ default and intermediate macros (all aliases).
  37. * - The selection framework also caused many macro aliases.
  38. * - Change in Unicode standard: "irregular" sequences (3.0) became illegal (3.2).
  39. * - Change of language in Unicode standard:
  40. * Growing distinction between internal x-bit Unicode strings and external UTF-x
  41. * forms, with the former more lenient.
  42. * Suggests renaming of UTF16_ macros to U16_.
  43. * - The prefix "UTF_" without a width number confused some users.
  44. * - "Safe" append macros needed the addition of an error indicator output.
  45. * - "Safe" UTF-8 macros used legitimate (if rarely used) code point values
  46. * to indicate error conditions.
  47. * - The use of the "_CHAR" infix for code point operations confused some users.
  48. *
  49. * More details:
  50. *
  51. * Until ICU 2.2, utf.h theoretically allowed to choose among UTF-8/16/32
  52. * for string processing, and among unsafe/safe/strict default macros for that.
  53. *
  54. * It proved nearly impossible to write non-trivial, high-performance code
  55. * that is UTF-generic.
  56. * Unsafe default macros would be dangerous for default string processing,
  57. * and the main reason for the "strict" versions disappeared:
  58. * Between Unicode 3.0 and 3.2 all "irregular" UTF-8 sequences became illegal.
  59. * The only other conditions that "strict" checked for were non-characters,
  60. * which are valid during processing. Only during text input/output should they
  61. * be checked, and at that time other well-formedness checks may be
  62. * necessary or useful as well.
  63. * This can still be done by using U16_NEXT and U_IS_UNICODE_NONCHAR
  64. * or U_IS_UNICODE_CHAR.
  65. *
  66. * The old UTF8_..._SAFE macros also used some normal Unicode code points
  67. * to indicate malformed sequences.
  68. * The new UTF8_ macros without suffix use negative values instead.
  69. *
  70. * The entire contents of utf32.h was moved here without replacement
  71. * because all those macros were trivial and
  72. * were meaningful only in the framework of choosing the UTF size.
  73. *
  74. * See Jitterbug 2150 and its discussion on the ICU mailing list
  75. * in September 2002.
  76. *
  77. * <hr>
  78. *
  79. * <em>Obsolete part</em> of pre-ICU 2.4 utf.h file documentation:
  80. *
  81. * <p>The original concept for these files was for ICU to allow
  82. * in principle to set which UTF (UTF-8/16/32) is used internally
  83. * by defining UTF_SIZE to either 8, 16, or 32. utf.h would then define the UChar type
  84. * accordingly. UTF-16 was the default.</p>
  85. *
  86. * <p>This concept has been abandoned.
  87. * A lot of the ICU source code assumes UChar strings are in UTF-16.
  88. * This is especially true for low-level code like
  89. * conversion, normalization, and collation.
  90. * The utf.h header enforces the default of UTF-16.
  91. * The UTF-8 and UTF-32 macros remain for now for completeness and backward compatibility.</p>
  92. *
  93. * <p>Accordingly, utf.h defines UChar to be an unsigned 16-bit integer. If this matches wchar_t, then
  94. * UChar is defined to be exactly wchar_t, otherwise uint16_t.</p>
  95. *
  96. * <p>UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit
  97. * Unicode code point (Unicode scalar value, 0..0x10ffff).
  98. * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as
  99. * the definition of UChar. For details see the documentation for UChar32 itself.</p>
  100. *
  101. * <p>utf.h also defines a number of C macros for handling single Unicode code points and
  102. * for using UTF Unicode strings. It includes utf8.h, utf16.h, and utf32.h for the actual
  103. * implementations of those macros and then aliases one set of them (for UTF-16) for general use.
  104. * The UTF-specific macros have the UTF size in the macro name prefixes (UTF16_...), while
  105. * the general alias macros always begin with UTF_...</p>
  106. *
  107. * <p>Many string operations can be done with or without error checking.
  108. * Where such a distinction is useful, there are two versions of the macros, "unsafe" and "safe"
  109. * ones with ..._UNSAFE and ..._SAFE suffixes. The unsafe macros are fast but may cause
  110. * program failures if the strings are not well-formed. The safe macros have an additional, boolean
  111. * parameter "strict". If strict is false, then only illegal sequences are detected.
  112. * Otherwise, irregular sequences and non-characters are detected as well (like single surrogates).
  113. * Safe macros return special error code points for illegal/irregular sequences:
  114. * Typically, U+ffff, or values that would result in a code unit sequence of the same length
  115. * as the erroneous input sequence.<br>
  116. * Note that _UNSAFE macros have fewer parameters: They do not have the strictness parameter, and
  117. * they do not have start/length parameters for boundary checking.</p>
  118. *
  119. * <p>Here, the macros are aliased in two steps:
  120. * In the first step, the UTF-specific macros with UTF16_ prefix and _UNSAFE and _SAFE suffixes are
  121. * aliased according to the UTF_SIZE to macros with UTF_ prefix and the same suffixes and signatures.
  122. * Then, in a second step, the default, general alias macros are set to use either the unsafe or
  123. * the safe/not strict (default) or the safe/strict macro;
  124. * these general macros do not have a strictness parameter.</p>
  125. *
  126. * <p>It is possible to change the default choice for the general alias macros to be unsafe, safe/not strict or safe/strict.
  127. * The default is safe/not strict. It is not recommended to select the unsafe macros as the basis for
  128. * Unicode string handling in ICU! To select this, define UTF_SAFE, UTF_STRICT, or UTF_UNSAFE.</p>
  129. *
  130. * <p>For general use, one should use the default, general macros with UTF_ prefix and no _SAFE/_UNSAFE suffix.
  131. * Only in some cases it may be necessary to control the choice of macro directly and use a less generic alias.
  132. * For example, if it can be assumed that a string is well-formed and the index will stay within the bounds,
  133. * then the _UNSAFE version may be used.
  134. * If a UTF-8 string is to be processed, then the macros with UTF8_ prefixes need to be used.</p>
  135. *
  136. * <hr>
  137. *
  138. * Deprecated ICU 2.4. Use the macros in utf.h, utf16.h, utf8.h instead.
  139. */
  140. #ifndef __UTF_OLD_H__
  141. #define __UTF_OLD_H__
  142. #include "unicode/utf.h"
  143. #include "unicode/utf8.h"
  144. #include "unicode/utf16.h"
  145. /**
  146. * \def U_HIDE_OBSOLETE_UTF_OLD_H
  147. *
  148. * Hides the obsolete definitions in unicode/utf_old.h.
  149. * Recommended to be set to 1 at compile time to make sure
  150. * the long-deprecated macros are no longer used.
  151. *
  152. * For reasons for the deprecation see the utf_old.h file comments.
  153. *
  154. * @internal
  155. */
  156. #ifndef U_HIDE_OBSOLETE_UTF_OLD_H
  157. # define U_HIDE_OBSOLETE_UTF_OLD_H 0
  158. #endif
  159. #if !defined(U_HIDE_DEPRECATED_API) && !U_HIDE_OBSOLETE_UTF_OLD_H
  160. /* Formerly utf.h, part 1 --------------------------------------------------- */
  161. #ifdef U_USE_UTF_DEPRECATES
  162. /**
  163. * Unicode string and array offset and index type.
  164. * ICU always counts Unicode code units (UChars) for
  165. * string offsets, indexes, and lengths, not Unicode code points.
  166. *
  167. * @obsolete ICU 2.6. Use int32_t directly instead since this API will be removed in that release.
  168. */
  169. typedef int32_t UTextOffset;
  170. #endif
  171. /** Number of bits in a Unicode string code unit - ICU uses 16-bit Unicode. @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  172. #define UTF_SIZE 16
  173. /**
  174. * The default choice for general Unicode string macros is to use the ..._SAFE macro implementations
  175. * with strict=false.
  176. *
  177. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  178. */
  179. #define UTF_SAFE
  180. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  181. #undef UTF_UNSAFE
  182. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  183. #undef UTF_STRICT
  184. /**
  185. * UTF8_ERROR_VALUE_1 and UTF8_ERROR_VALUE_2 are special error values for UTF-8,
  186. * which need 1 or 2 bytes in UTF-8:
  187. * \code
  188. * U+0015 = NAK = Negative Acknowledge, C0 control character
  189. * U+009f = highest C1 control character
  190. * \endcode
  191. *
  192. * These are used by UTF8_..._SAFE macros so that they can return an error value
  193. * that needs the same number of code units (bytes) as were seen by
  194. * a macro. They should be tested with UTF_IS_ERROR() or UTF_IS_VALID().
  195. *
  196. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  197. */
  198. #define UTF8_ERROR_VALUE_1 0x15
  199. /**
  200. * See documentation on UTF8_ERROR_VALUE_1 for details.
  201. *
  202. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  203. */
  204. #define UTF8_ERROR_VALUE_2 0x9f
  205. /**
  206. * Error value for all UTFs. This code point value will be set by macros with error
  207. * checking if an error is detected.
  208. *
  209. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  210. */
  211. #define UTF_ERROR_VALUE 0xffff
  212. /**
  213. * Is a given 32-bit code an error value
  214. * as returned by one of the macros for any UTF?
  215. *
  216. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  217. */
  218. #define UTF_IS_ERROR(c) \
  219. (((c)&0xfffe)==0xfffe || (c)==UTF8_ERROR_VALUE_1 || (c)==UTF8_ERROR_VALUE_2)
  220. /**
  221. * This is a combined macro: Is c a valid Unicode value _and_ not an error code?
  222. *
  223. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  224. */
  225. #define UTF_IS_VALID(c) \
  226. (UTF_IS_UNICODE_CHAR(c) && \
  227. (c)!=UTF8_ERROR_VALUE_1 && (c)!=UTF8_ERROR_VALUE_2)
  228. /**
  229. * Is this code unit or code point a surrogate (U+d800..U+dfff)?
  230. * @deprecated ICU 2.4. Renamed to U_IS_SURROGATE and U16_IS_SURROGATE, see utf_old.h.
  231. */
  232. #define UTF_IS_SURROGATE(uchar) (((uchar)&0xfffff800)==0xd800)
  233. /**
  234. * Is a given 32-bit code point a Unicode noncharacter?
  235. *
  236. * @deprecated ICU 2.4. Renamed to U_IS_UNICODE_NONCHAR, see utf_old.h.
  237. */
  238. #define UTF_IS_UNICODE_NONCHAR(c) \
  239. ((c)>=0xfdd0 && \
  240. ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \
  241. (uint32_t)(c)<=0x10ffff)
  242. /**
  243. * Is a given 32-bit value a Unicode code point value (0..U+10ffff)
  244. * that can be assigned a character?
  245. *
  246. * Code points that are not characters include:
  247. * - single surrogate code points (U+d800..U+dfff, 2048 code points)
  248. * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
  249. * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
  250. * - the highest Unicode code point value is U+10ffff
  251. *
  252. * This means that all code points below U+d800 are character code points,
  253. * and that boundary is tested first for performance.
  254. *
  255. * @deprecated ICU 2.4. Renamed to U_IS_UNICODE_CHAR, see utf_old.h.
  256. */
  257. #define UTF_IS_UNICODE_CHAR(c) \
  258. ((uint32_t)(c)<0xd800 || \
  259. ((uint32_t)(c)>0xdfff && \
  260. (uint32_t)(c)<=0x10ffff && \
  261. !UTF_IS_UNICODE_NONCHAR(c)))
  262. /* Formerly utf8.h ---------------------------------------------------------- */
  263. /**
  264. * \var utf8_countTrailBytes
  265. * Internal array with numbers of trail bytes for any given byte used in
  266. * lead byte position.
  267. *
  268. * This is internal since it is not meant to be called directly by external clients;
  269. * however it is called by public macros in this file and thus must remain stable,
  270. * and should not be hidden when other internal functions are hidden (otherwise
  271. * public macros would fail to compile).
  272. * @internal
  273. */
  274. #ifdef U_UTF8_IMPL
  275. // No forward declaration if compiling utf_impl.cpp, which defines utf8_countTrailBytes.
  276. #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
  277. U_CAPI const uint8_t utf8_countTrailBytes[];
  278. #else
  279. U_CFUNC U_IMPORT const uint8_t utf8_countTrailBytes[];
  280. #endif
  281. /**
  282. * Count the trail bytes for a UTF-8 lead byte.
  283. * @deprecated ICU 2.4. Renamed to U8_COUNT_TRAIL_BYTES, see utf_old.h.
  284. */
  285. #define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
  286. /**
  287. * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
  288. * @deprecated ICU 2.4. Renamed to U8_MASK_LEAD_BYTE, see utf_old.h.
  289. */
  290. #define UTF8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
  291. /** Is this this code point a single code unit (byte)? @deprecated ICU 2.4. Renamed to U8_IS_SINGLE, see utf_old.h. */
  292. #define UTF8_IS_SINGLE(uchar) (((uchar)&0x80)==0)
  293. /** Is this this code unit the lead code unit (byte) of a code point? @deprecated ICU 2.4. Renamed to U8_IS_LEAD, see utf_old.h. */
  294. #define UTF8_IS_LEAD(uchar) ((uint8_t)((uchar)-0xc0)<0x3e)
  295. /** Is this this code unit a trailing code unit (byte) of a code point? @deprecated ICU 2.4. Renamed to U8_IS_TRAIL, see utf_old.h. */
  296. #define UTF8_IS_TRAIL(uchar) (((uchar)&0xc0)==0x80)
  297. /** Does this scalar Unicode value need multiple code units for storage? @deprecated ICU 2.4. Use U8_LENGTH or test ((uint32_t)(c)>0x7f) instead, see utf_old.h. */
  298. #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
  299. /**
  300. * Given the lead character, how many bytes are taken by this code point.
  301. * ICU does not deal with code points >0x10ffff
  302. * unless necessary for advancing in the byte stream.
  303. *
  304. * These length macros take into account that for values >0x10ffff
  305. * the UTF8_APPEND_CHAR_SAFE macros would write the error code point 0xffff
  306. * with 3 bytes.
  307. * Code point comparisons need to be in uint32_t because UChar32
  308. * may be a signed type, and negative values must be recognized.
  309. *
  310. * @deprecated ICU 2.4. Use U8_LENGTH instead, see utf.h.
  311. */
  312. #if 1
  313. # define UTF8_CHAR_LENGTH(c) \
  314. ((uint32_t)(c)<=0x7f ? 1 : \
  315. ((uint32_t)(c)<=0x7ff ? 2 : \
  316. ((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \
  317. ) \
  318. )
  319. #else
  320. # define UTF8_CHAR_LENGTH(c) \
  321. ((uint32_t)(c)<=0x7f ? 1 : \
  322. ((uint32_t)(c)<=0x7ff ? 2 : \
  323. ((uint32_t)(c)<=0xffff ? 3 : \
  324. ((uint32_t)(c)<=0x10ffff ? 4 : \
  325. ((uint32_t)(c)<=0x3ffffff ? 5 : \
  326. ((uint32_t)(c)<=0x7fffffff ? 6 : 3) \
  327. ) \
  328. ) \
  329. ) \
  330. ) \
  331. )
  332. #endif
  333. /** The maximum number of bytes per code point. @deprecated ICU 2.4. Renamed to U8_MAX_LENGTH, see utf_old.h. */
  334. #define UTF8_MAX_CHAR_LENGTH 4
  335. /** Average number of code units compared to UTF-16. @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  336. #define UTF8_ARRAY_SIZE(size) ((5*(size))/2)
  337. /** @deprecated ICU 2.4. Renamed to U8_GET_UNSAFE, see utf_old.h. */
  338. #define UTF8_GET_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  339. int32_t _utf8_get_char_unsafe_index=(int32_t)(i); \
  340. UTF8_SET_CHAR_START_UNSAFE(s, _utf8_get_char_unsafe_index); \
  341. UTF8_NEXT_CHAR_UNSAFE(s, _utf8_get_char_unsafe_index, c); \
  342. } UPRV_BLOCK_MACRO_END
  343. /** @deprecated ICU 2.4. Use U8_GET instead, see utf_old.h. */
  344. #define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  345. int32_t _utf8_get_char_safe_index=(int32_t)(i); \
  346. UTF8_SET_CHAR_START_SAFE(s, start, _utf8_get_char_safe_index); \
  347. UTF8_NEXT_CHAR_SAFE(s, _utf8_get_char_safe_index, length, c, strict); \
  348. } UPRV_BLOCK_MACRO_END
  349. /** @deprecated ICU 2.4. Renamed to U8_NEXT_UNSAFE, see utf_old.h. */
  350. #define UTF8_NEXT_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  351. (c)=(s)[(i)++]; \
  352. if((uint8_t)((c)-0xc0)<0x35) { \
  353. uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
  354. UTF8_MASK_LEAD_BYTE(c, __count); \
  355. switch(__count) { \
  356. /* each following branch falls through to the next one */ \
  357. case 3: \
  358. (c)=((c)<<6)|((s)[(i)++]&0x3f); \
  359. case 2: \
  360. (c)=((c)<<6)|((s)[(i)++]&0x3f); \
  361. case 1: \
  362. (c)=((c)<<6)|((s)[(i)++]&0x3f); \
  363. /* no other branches to optimize switch() */ \
  364. break; \
  365. } \
  366. } \
  367. } UPRV_BLOCK_MACRO_END
  368. /** @deprecated ICU 2.4. Renamed to U8_APPEND_UNSAFE, see utf_old.h. */
  369. #define UTF8_APPEND_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  370. if((uint32_t)(c)<=0x7f) { \
  371. (s)[(i)++]=(uint8_t)(c); \
  372. } else { \
  373. if((uint32_t)(c)<=0x7ff) { \
  374. (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
  375. } else { \
  376. if((uint32_t)(c)<=0xffff) { \
  377. (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
  378. } else { \
  379. (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
  380. (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
  381. } \
  382. (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
  383. } \
  384. (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
  385. } \
  386. } UPRV_BLOCK_MACRO_END
  387. /** @deprecated ICU 2.4. Renamed to U8_FWD_1_UNSAFE, see utf_old.h. */
  388. #define UTF8_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  389. (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
  390. } UPRV_BLOCK_MACRO_END
  391. /** @deprecated ICU 2.4. Renamed to U8_FWD_N_UNSAFE, see utf_old.h. */
  392. #define UTF8_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  393. int32_t __N=(n); \
  394. while(__N>0) { \
  395. UTF8_FWD_1_UNSAFE(s, i); \
  396. --__N; \
  397. } \
  398. } UPRV_BLOCK_MACRO_END
  399. /** @deprecated ICU 2.4. Renamed to U8_SET_CP_START_UNSAFE, see utf_old.h. */
  400. #define UTF8_SET_CHAR_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  401. while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
  402. } UPRV_BLOCK_MACRO_END
  403. /** @deprecated ICU 2.4. Use U8_NEXT instead, see utf_old.h. */
  404. #define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  405. (c)=(s)[(i)++]; \
  406. if((c)>=0x80) { \
  407. if(UTF8_IS_LEAD(c)) { \
  408. (c)=utf8_nextCharSafeBody(s, &(i), (int32_t)(length), c, strict); \
  409. } else { \
  410. (c)=UTF8_ERROR_VALUE_1; \
  411. } \
  412. } \
  413. } UPRV_BLOCK_MACRO_END
  414. /** @deprecated ICU 2.4. Use U8_APPEND instead, see utf_old.h. */
  415. #define UTF8_APPEND_CHAR_SAFE(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
  416. if((uint32_t)(c)<=0x7f) { \
  417. (s)[(i)++]=(uint8_t)(c); \
  418. } else { \
  419. (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c, NULL); \
  420. } \
  421. } UPRV_BLOCK_MACRO_END
  422. /** @deprecated ICU 2.4. Renamed to U8_FWD_1, see utf_old.h. */
  423. #define UTF8_FWD_1_SAFE(s, i, length) U8_FWD_1(s, i, length)
  424. /** @deprecated ICU 2.4. Renamed to U8_FWD_N, see utf_old.h. */
  425. #define UTF8_FWD_N_SAFE(s, i, length, n) U8_FWD_N(s, i, length, n)
  426. /** @deprecated ICU 2.4. Renamed to U8_SET_CP_START, see utf_old.h. */
  427. #define UTF8_SET_CHAR_START_SAFE(s, start, i) U8_SET_CP_START(s, start, i)
  428. /** @deprecated ICU 2.4. Renamed to U8_PREV_UNSAFE, see utf_old.h. */
  429. #define UTF8_PREV_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  430. (c)=(s)[--(i)]; \
  431. if(UTF8_IS_TRAIL(c)) { \
  432. uint8_t __b, __count=1, __shift=6; \
  433. \
  434. /* c is a trail byte */ \
  435. (c)&=0x3f; \
  436. for(;;) { \
  437. __b=(s)[--(i)]; \
  438. if(__b>=0xc0) { \
  439. UTF8_MASK_LEAD_BYTE(__b, __count); \
  440. (c)|=(UChar32)__b<<__shift; \
  441. break; \
  442. } else { \
  443. (c)|=(UChar32)(__b&0x3f)<<__shift; \
  444. ++__count; \
  445. __shift+=6; \
  446. } \
  447. } \
  448. } \
  449. } UPRV_BLOCK_MACRO_END
  450. /** @deprecated ICU 2.4. Renamed to U8_BACK_1_UNSAFE, see utf_old.h. */
  451. #define UTF8_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  452. while(UTF8_IS_TRAIL((s)[--(i)])) {} \
  453. } UPRV_BLOCK_MACRO_END
  454. /** @deprecated ICU 2.4. Renamed to U8_BACK_N_UNSAFE, see utf_old.h. */
  455. #define UTF8_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  456. int32_t __N=(n); \
  457. while(__N>0) { \
  458. UTF8_BACK_1_UNSAFE(s, i); \
  459. --__N; \
  460. } \
  461. } UPRV_BLOCK_MACRO_END
  462. /** @deprecated ICU 2.4. Renamed to U8_SET_CP_LIMIT_UNSAFE, see utf_old.h. */
  463. #define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  464. UTF8_BACK_1_UNSAFE(s, i); \
  465. UTF8_FWD_1_UNSAFE(s, i); \
  466. } UPRV_BLOCK_MACRO_END
  467. /** @deprecated ICU 2.4. Use U8_PREV instead, see utf_old.h. */
  468. #define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  469. (c)=(s)[--(i)]; \
  470. if((c)>=0x80) { \
  471. if((c)<=0xbf) { \
  472. (c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
  473. } else { \
  474. (c)=UTF8_ERROR_VALUE_1; \
  475. } \
  476. } \
  477. } UPRV_BLOCK_MACRO_END
  478. /** @deprecated ICU 2.4. Renamed to U8_BACK_1, see utf_old.h. */
  479. #define UTF8_BACK_1_SAFE(s, start, i) U8_BACK_1(s, start, i)
  480. /** @deprecated ICU 2.4. Renamed to U8_BACK_N, see utf_old.h. */
  481. #define UTF8_BACK_N_SAFE(s, start, i, n) U8_BACK_N(s, start, i, n)
  482. /** @deprecated ICU 2.4. Renamed to U8_SET_CP_LIMIT, see utf_old.h. */
  483. #define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length) U8_SET_CP_LIMIT(s, start, i, length)
  484. /* Formerly utf16.h --------------------------------------------------------- */
  485. /** Is uchar a first/lead surrogate? @deprecated ICU 2.4. Renamed to U_IS_LEAD and U16_IS_LEAD, see utf_old.h. */
  486. #define UTF_IS_FIRST_SURROGATE(uchar) (((uchar)&0xfffffc00)==0xd800)
  487. /** Is uchar a second/trail surrogate? @deprecated ICU 2.4. Renamed to U_IS_TRAIL and U16_IS_TRAIL, see utf_old.h. */
  488. #define UTF_IS_SECOND_SURROGATE(uchar) (((uchar)&0xfffffc00)==0xdc00)
  489. /** Assuming c is a surrogate, is it a first/lead surrogate? @deprecated ICU 2.4. Renamed to U_IS_SURROGATE_LEAD and U16_IS_SURROGATE_LEAD, see utf_old.h. */
  490. #define UTF_IS_SURROGATE_FIRST(c) (((c)&0x400)==0)
  491. /** Helper constant for UTF16_GET_PAIR_VALUE. @deprecated ICU 2.4. Renamed to U16_SURROGATE_OFFSET, see utf_old.h. */
  492. #define UTF_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
  493. /** Get the UTF-32 value from the surrogate code units. @deprecated ICU 2.4. Renamed to U16_GET_SUPPLEMENTARY, see utf_old.h. */
  494. #define UTF16_GET_PAIR_VALUE(first, second) \
  495. (((first)<<10UL)+(second)-UTF_SURROGATE_OFFSET)
  496. /** @deprecated ICU 2.4. Renamed to U16_LEAD, see utf_old.h. */
  497. #define UTF_FIRST_SURROGATE(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
  498. /** @deprecated ICU 2.4. Renamed to U16_TRAIL, see utf_old.h. */
  499. #define UTF_SECOND_SURROGATE(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
  500. /** @deprecated ICU 2.4. Renamed to U16_LEAD, see utf_old.h. */
  501. #define UTF16_LEAD(supplementary) UTF_FIRST_SURROGATE(supplementary)
  502. /** @deprecated ICU 2.4. Renamed to U16_TRAIL, see utf_old.h. */
  503. #define UTF16_TRAIL(supplementary) UTF_SECOND_SURROGATE(supplementary)
  504. /** @deprecated ICU 2.4. Renamed to U16_IS_SINGLE, see utf_old.h. */
  505. #define UTF16_IS_SINGLE(uchar) !UTF_IS_SURROGATE(uchar)
  506. /** @deprecated ICU 2.4. Renamed to U16_IS_LEAD, see utf_old.h. */
  507. #define UTF16_IS_LEAD(uchar) UTF_IS_FIRST_SURROGATE(uchar)
  508. /** @deprecated ICU 2.4. Renamed to U16_IS_TRAIL, see utf_old.h. */
  509. #define UTF16_IS_TRAIL(uchar) UTF_IS_SECOND_SURROGATE(uchar)
  510. /** Does this scalar Unicode value need multiple code units for storage? @deprecated ICU 2.4. Use U16_LENGTH or test ((uint32_t)(c)>0xffff) instead, see utf_old.h. */
  511. #define UTF16_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0xffff)
  512. /** @deprecated ICU 2.4. Renamed to U16_LENGTH, see utf_old.h. */
  513. #define UTF16_CHAR_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
  514. /** @deprecated ICU 2.4. Renamed to U16_MAX_LENGTH, see utf_old.h. */
  515. #define UTF16_MAX_CHAR_LENGTH 2
  516. /** Average number of code units compared to UTF-16. @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  517. #define UTF16_ARRAY_SIZE(size) (size)
  518. /**
  519. * Get a single code point from an offset that points to any
  520. * of the code units that belong to that code point.
  521. * Assume 0<=i<length.
  522. *
  523. * This could be used for iteration together with
  524. * UTF16_CHAR_LENGTH() and UTF_IS_ERROR(),
  525. * but the use of UTF16_NEXT_CHAR[_UNSAFE]() and
  526. * UTF16_PREV_CHAR[_UNSAFE]() is more efficient for that.
  527. * @deprecated ICU 2.4. Renamed to U16_GET_UNSAFE, see utf_old.h.
  528. */
  529. #define UTF16_GET_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  530. (c)=(s)[i]; \
  531. if(UTF_IS_SURROGATE(c)) { \
  532. if(UTF_IS_SURROGATE_FIRST(c)) { \
  533. (c)=UTF16_GET_PAIR_VALUE((c), (s)[(i)+1]); \
  534. } else { \
  535. (c)=UTF16_GET_PAIR_VALUE((s)[(i)-1], (c)); \
  536. } \
  537. } \
  538. } UPRV_BLOCK_MACRO_END
  539. /** @deprecated ICU 2.4. Use U16_GET instead, see utf_old.h. */
  540. #define UTF16_GET_CHAR_SAFE(s, start, i, length, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  541. (c)=(s)[i]; \
  542. if(UTF_IS_SURROGATE(c)) { \
  543. uint16_t __c2; \
  544. if(UTF_IS_SURROGATE_FIRST(c)) { \
  545. if((i)+1<(length) && UTF_IS_SECOND_SURROGATE(__c2=(s)[(i)+1])) { \
  546. (c)=UTF16_GET_PAIR_VALUE((c), __c2); \
  547. /* strict: ((c)&0xfffe)==0xfffe is caught by UTF_IS_ERROR() and UTF_IS_UNICODE_CHAR() */ \
  548. } else if(strict) {\
  549. /* unmatched first surrogate */ \
  550. (c)=UTF_ERROR_VALUE; \
  551. } \
  552. } else { \
  553. if((i)-1>=(start) && UTF_IS_FIRST_SURROGATE(__c2=(s)[(i)-1])) { \
  554. (c)=UTF16_GET_PAIR_VALUE(__c2, (c)); \
  555. /* strict: ((c)&0xfffe)==0xfffe is caught by UTF_IS_ERROR() and UTF_IS_UNICODE_CHAR() */ \
  556. } else if(strict) {\
  557. /* unmatched second surrogate */ \
  558. (c)=UTF_ERROR_VALUE; \
  559. } \
  560. } \
  561. } else if((strict) && !UTF_IS_UNICODE_CHAR(c)) { \
  562. (c)=UTF_ERROR_VALUE; \
  563. } \
  564. } UPRV_BLOCK_MACRO_END
  565. /** @deprecated ICU 2.4. Renamed to U16_NEXT_UNSAFE, see utf_old.h. */
  566. #define UTF16_NEXT_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  567. (c)=(s)[(i)++]; \
  568. if(UTF_IS_FIRST_SURROGATE(c)) { \
  569. (c)=UTF16_GET_PAIR_VALUE((c), (s)[(i)++]); \
  570. } \
  571. } UPRV_BLOCK_MACRO_END
  572. /** @deprecated ICU 2.4. Renamed to U16_APPEND_UNSAFE, see utf_old.h. */
  573. #define UTF16_APPEND_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  574. if((uint32_t)(c)<=0xffff) { \
  575. (s)[(i)++]=(uint16_t)(c); \
  576. } else { \
  577. (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
  578. (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
  579. } \
  580. } UPRV_BLOCK_MACRO_END
  581. /** @deprecated ICU 2.4. Renamed to U16_FWD_1_UNSAFE, see utf_old.h. */
  582. #define UTF16_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  583. if(UTF_IS_FIRST_SURROGATE((s)[(i)++])) { \
  584. ++(i); \
  585. } \
  586. } UPRV_BLOCK_MACRO_END
  587. /** @deprecated ICU 2.4. Renamed to U16_FWD_N_UNSAFE, see utf_old.h. */
  588. #define UTF16_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  589. int32_t __N=(n); \
  590. while(__N>0) { \
  591. UTF16_FWD_1_UNSAFE(s, i); \
  592. --__N; \
  593. } \
  594. } UPRV_BLOCK_MACRO_END
  595. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_START_UNSAFE, see utf_old.h. */
  596. #define UTF16_SET_CHAR_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  597. if(UTF_IS_SECOND_SURROGATE((s)[i])) { \
  598. --(i); \
  599. } \
  600. } UPRV_BLOCK_MACRO_END
  601. /** @deprecated ICU 2.4. Use U16_NEXT instead, see utf_old.h. */
  602. #define UTF16_NEXT_CHAR_SAFE(s, i, length, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  603. (c)=(s)[(i)++]; \
  604. if(UTF_IS_FIRST_SURROGATE(c)) { \
  605. uint16_t __c2; \
  606. if((i)<(length) && UTF_IS_SECOND_SURROGATE(__c2=(s)[(i)])) { \
  607. ++(i); \
  608. (c)=UTF16_GET_PAIR_VALUE((c), __c2); \
  609. /* strict: ((c)&0xfffe)==0xfffe is caught by UTF_IS_ERROR() and UTF_IS_UNICODE_CHAR() */ \
  610. } else if(strict) {\
  611. /* unmatched first surrogate */ \
  612. (c)=UTF_ERROR_VALUE; \
  613. } \
  614. } else if((strict) && !UTF_IS_UNICODE_CHAR(c)) { \
  615. /* unmatched second surrogate or other non-character */ \
  616. (c)=UTF_ERROR_VALUE; \
  617. } \
  618. } UPRV_BLOCK_MACRO_END
  619. /** @deprecated ICU 2.4. Use U16_APPEND instead, see utf_old.h. */
  620. #define UTF16_APPEND_CHAR_SAFE(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
  621. if((uint32_t)(c)<=0xffff) { \
  622. (s)[(i)++]=(uint16_t)(c); \
  623. } else if((uint32_t)(c)<=0x10ffff) { \
  624. if((i)+1<(length)) { \
  625. (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
  626. (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
  627. } else /* not enough space */ { \
  628. (s)[(i)++]=UTF_ERROR_VALUE; \
  629. } \
  630. } else /* c>0x10ffff, write error value */ { \
  631. (s)[(i)++]=UTF_ERROR_VALUE; \
  632. } \
  633. } UPRV_BLOCK_MACRO_END
  634. /** @deprecated ICU 2.4. Renamed to U16_FWD_1, see utf_old.h. */
  635. #define UTF16_FWD_1_SAFE(s, i, length) U16_FWD_1(s, i, length)
  636. /** @deprecated ICU 2.4. Renamed to U16_FWD_N, see utf_old.h. */
  637. #define UTF16_FWD_N_SAFE(s, i, length, n) U16_FWD_N(s, i, length, n)
  638. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_START, see utf_old.h. */
  639. #define UTF16_SET_CHAR_START_SAFE(s, start, i) U16_SET_CP_START(s, start, i)
  640. /** @deprecated ICU 2.4. Renamed to U16_PREV_UNSAFE, see utf_old.h. */
  641. #define UTF16_PREV_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  642. (c)=(s)[--(i)]; \
  643. if(UTF_IS_SECOND_SURROGATE(c)) { \
  644. (c)=UTF16_GET_PAIR_VALUE((s)[--(i)], (c)); \
  645. } \
  646. } UPRV_BLOCK_MACRO_END
  647. /** @deprecated ICU 2.4. Renamed to U16_BACK_1_UNSAFE, see utf_old.h. */
  648. #define UTF16_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  649. if(UTF_IS_SECOND_SURROGATE((s)[--(i)])) { \
  650. --(i); \
  651. } \
  652. } UPRV_BLOCK_MACRO_END
  653. /** @deprecated ICU 2.4. Renamed to U16_BACK_N_UNSAFE, see utf_old.h. */
  654. #define UTF16_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  655. int32_t __N=(n); \
  656. while(__N>0) { \
  657. UTF16_BACK_1_UNSAFE(s, i); \
  658. --__N; \
  659. } \
  660. } UPRV_BLOCK_MACRO_END
  661. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_LIMIT_UNSAFE, see utf_old.h. */
  662. #define UTF16_SET_CHAR_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  663. if(UTF_IS_FIRST_SURROGATE((s)[(i)-1])) { \
  664. ++(i); \
  665. } \
  666. } UPRV_BLOCK_MACRO_END
  667. /** @deprecated ICU 2.4. Use U16_PREV instead, see utf_old.h. */
  668. #define UTF16_PREV_CHAR_SAFE(s, start, i, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  669. (c)=(s)[--(i)]; \
  670. if(UTF_IS_SECOND_SURROGATE(c)) { \
  671. uint16_t __c2; \
  672. if((i)>(start) && UTF_IS_FIRST_SURROGATE(__c2=(s)[(i)-1])) { \
  673. --(i); \
  674. (c)=UTF16_GET_PAIR_VALUE(__c2, (c)); \
  675. /* strict: ((c)&0xfffe)==0xfffe is caught by UTF_IS_ERROR() and UTF_IS_UNICODE_CHAR() */ \
  676. } else if(strict) {\
  677. /* unmatched second surrogate */ \
  678. (c)=UTF_ERROR_VALUE; \
  679. } \
  680. } else if((strict) && !UTF_IS_UNICODE_CHAR(c)) { \
  681. /* unmatched first surrogate or other non-character */ \
  682. (c)=UTF_ERROR_VALUE; \
  683. } \
  684. } UPRV_BLOCK_MACRO_END
  685. /** @deprecated ICU 2.4. Renamed to U16_BACK_1, see utf_old.h. */
  686. #define UTF16_BACK_1_SAFE(s, start, i) U16_BACK_1(s, start, i)
  687. /** @deprecated ICU 2.4. Renamed to U16_BACK_N, see utf_old.h. */
  688. #define UTF16_BACK_N_SAFE(s, start, i, n) U16_BACK_N(s, start, i, n)
  689. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_LIMIT, see utf_old.h. */
  690. #define UTF16_SET_CHAR_LIMIT_SAFE(s, start, i, length) U16_SET_CP_LIMIT(s, start, i, length)
  691. /* Formerly utf32.h --------------------------------------------------------- */
  692. /*
  693. * Old documentation:
  694. *
  695. * This file defines macros to deal with UTF-32 code units and code points.
  696. * Signatures and semantics are the same as for the similarly named macros
  697. * in utf16.h.
  698. * utf32.h is included by utf.h after unicode/umachine.h</p>
  699. * and some common definitions.
  700. * <p><b>Usage:</b> ICU coding guidelines for if() statements should be followed when using these macros.
  701. * Compound statements (curly braces {}) must be used for if-else-while...
  702. * bodies and all macro statements should be terminated with semicolon.</p>
  703. */
  704. /* internal definitions ----------------------------------------------------- */
  705. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  706. #define UTF32_IS_SAFE(c, strict) \
  707. (!(strict) ? \
  708. (uint32_t)(c)<=0x10ffff : \
  709. UTF_IS_UNICODE_CHAR(c))
  710. /*
  711. * For the semantics of all of these macros, see utf16.h.
  712. * The UTF-32 versions are trivial because any code point is
  713. * encoded using exactly one code unit.
  714. */
  715. /* single-code point definitions -------------------------------------------- */
  716. /* classes of code unit values */
  717. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  718. #define UTF32_IS_SINGLE(uchar) 1
  719. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  720. #define UTF32_IS_LEAD(uchar) 0
  721. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  722. #define UTF32_IS_TRAIL(uchar) 0
  723. /* number of code units per code point */
  724. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  725. #define UTF32_NEED_MULTIPLE_UCHAR(c) 0
  726. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  727. #define UTF32_CHAR_LENGTH(c) 1
  728. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  729. #define UTF32_MAX_CHAR_LENGTH 1
  730. /* average number of code units compared to UTF-16 */
  731. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  732. #define UTF32_ARRAY_SIZE(size) (size)
  733. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  734. #define UTF32_GET_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  735. (c)=(s)[i]; \
  736. } UPRV_BLOCK_MACRO_END
  737. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  738. #define UTF32_GET_CHAR_SAFE(s, start, i, length, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  739. (c)=(s)[i]; \
  740. if(!UTF32_IS_SAFE(c, strict)) { \
  741. (c)=UTF_ERROR_VALUE; \
  742. } \
  743. } UPRV_BLOCK_MACRO_END
  744. /* definitions with forward iteration --------------------------------------- */
  745. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  746. #define UTF32_NEXT_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  747. (c)=(s)[(i)++]; \
  748. } UPRV_BLOCK_MACRO_END
  749. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  750. #define UTF32_APPEND_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  751. (s)[(i)++]=(c); \
  752. } UPRV_BLOCK_MACRO_END
  753. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  754. #define UTF32_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  755. ++(i); \
  756. } UPRV_BLOCK_MACRO_END
  757. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  758. #define UTF32_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  759. (i)+=(n); \
  760. } UPRV_BLOCK_MACRO_END
  761. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  762. #define UTF32_SET_CHAR_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  763. } UPRV_BLOCK_MACRO_END
  764. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  765. #define UTF32_NEXT_CHAR_SAFE(s, i, length, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  766. (c)=(s)[(i)++]; \
  767. if(!UTF32_IS_SAFE(c, strict)) { \
  768. (c)=UTF_ERROR_VALUE; \
  769. } \
  770. } UPRV_BLOCK_MACRO_END
  771. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  772. #define UTF32_APPEND_CHAR_SAFE(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
  773. if((uint32_t)(c)<=0x10ffff) { \
  774. (s)[(i)++]=(c); \
  775. } else /* c>0x10ffff, write 0xfffd */ { \
  776. (s)[(i)++]=0xfffd; \
  777. } \
  778. } UPRV_BLOCK_MACRO_END
  779. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  780. #define UTF32_FWD_1_SAFE(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \
  781. ++(i); \
  782. } UPRV_BLOCK_MACRO_END
  783. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  784. #define UTF32_FWD_N_SAFE(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \
  785. if(((i)+=(n))>(length)) { \
  786. (i)=(length); \
  787. } \
  788. } UPRV_BLOCK_MACRO_END
  789. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  790. #define UTF32_SET_CHAR_START_SAFE(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
  791. } UPRV_BLOCK_MACRO_END
  792. /* definitions with backward iteration -------------------------------------- */
  793. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  794. #define UTF32_PREV_CHAR_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  795. (c)=(s)[--(i)]; \
  796. } UPRV_BLOCK_MACRO_END
  797. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  798. #define UTF32_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  799. --(i); \
  800. } UPRV_BLOCK_MACRO_END
  801. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  802. #define UTF32_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  803. (i)-=(n); \
  804. } UPRV_BLOCK_MACRO_END
  805. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  806. #define UTF32_SET_CHAR_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  807. } UPRV_BLOCK_MACRO_END
  808. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  809. #define UTF32_PREV_CHAR_SAFE(s, start, i, c, strict) UPRV_BLOCK_MACRO_BEGIN { \
  810. (c)=(s)[--(i)]; \
  811. if(!UTF32_IS_SAFE(c, strict)) { \
  812. (c)=UTF_ERROR_VALUE; \
  813. } \
  814. } UPRV_BLOCK_MACRO_END
  815. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  816. #define UTF32_BACK_1_SAFE(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
  817. --(i); \
  818. } UPRV_BLOCK_MACRO_END
  819. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  820. #define UTF32_BACK_N_SAFE(s, start, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  821. (i)-=(n); \
  822. if((i)<(start)) { \
  823. (i)=(start); \
  824. } \
  825. } UPRV_BLOCK_MACRO_END
  826. /** @deprecated ICU 2.4. Obsolete, see utf_old.h. */
  827. #define UTF32_SET_CHAR_LIMIT_SAFE(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \
  828. } UPRV_BLOCK_MACRO_END
  829. /* Formerly utf.h, part 2 --------------------------------------------------- */
  830. /**
  831. * Estimate the number of code units for a string based on the number of UTF-16 code units.
  832. *
  833. * @deprecated ICU 2.4. Obsolete, see utf_old.h.
  834. */
  835. #define UTF_ARRAY_SIZE(size) UTF16_ARRAY_SIZE(size)
  836. /** @deprecated ICU 2.4. Renamed to U16_GET_UNSAFE, see utf_old.h. */
  837. #define UTF_GET_CHAR_UNSAFE(s, i, c) UTF16_GET_CHAR_UNSAFE(s, i, c)
  838. /** @deprecated ICU 2.4. Use U16_GET instead, see utf_old.h. */
  839. #define UTF_GET_CHAR_SAFE(s, start, i, length, c, strict) UTF16_GET_CHAR_SAFE(s, start, i, length, c, strict)
  840. /** @deprecated ICU 2.4. Renamed to U16_NEXT_UNSAFE, see utf_old.h. */
  841. #define UTF_NEXT_CHAR_UNSAFE(s, i, c) UTF16_NEXT_CHAR_UNSAFE(s, i, c)
  842. /** @deprecated ICU 2.4. Use U16_NEXT instead, see utf_old.h. */
  843. #define UTF_NEXT_CHAR_SAFE(s, i, length, c, strict) UTF16_NEXT_CHAR_SAFE(s, i, length, c, strict)
  844. /** @deprecated ICU 2.4. Renamed to U16_APPEND_UNSAFE, see utf_old.h. */
  845. #define UTF_APPEND_CHAR_UNSAFE(s, i, c) UTF16_APPEND_CHAR_UNSAFE(s, i, c)
  846. /** @deprecated ICU 2.4. Use U16_APPEND instead, see utf_old.h. */
  847. #define UTF_APPEND_CHAR_SAFE(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
  848. /** @deprecated ICU 2.4. Renamed to U16_FWD_1_UNSAFE, see utf_old.h. */
  849. #define UTF_FWD_1_UNSAFE(s, i) UTF16_FWD_1_UNSAFE(s, i)
  850. /** @deprecated ICU 2.4. Renamed to U16_FWD_1, see utf_old.h. */
  851. #define UTF_FWD_1_SAFE(s, i, length) UTF16_FWD_1_SAFE(s, i, length)
  852. /** @deprecated ICU 2.4. Renamed to U16_FWD_N_UNSAFE, see utf_old.h. */
  853. #define UTF_FWD_N_UNSAFE(s, i, n) UTF16_FWD_N_UNSAFE(s, i, n)
  854. /** @deprecated ICU 2.4. Renamed to U16_FWD_N, see utf_old.h. */
  855. #define UTF_FWD_N_SAFE(s, i, length, n) UTF16_FWD_N_SAFE(s, i, length, n)
  856. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_START_UNSAFE, see utf_old.h. */
  857. #define UTF_SET_CHAR_START_UNSAFE(s, i) UTF16_SET_CHAR_START_UNSAFE(s, i)
  858. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_START, see utf_old.h. */
  859. #define UTF_SET_CHAR_START_SAFE(s, start, i) UTF16_SET_CHAR_START_SAFE(s, start, i)
  860. /** @deprecated ICU 2.4. Renamed to U16_PREV_UNSAFE, see utf_old.h. */
  861. #define UTF_PREV_CHAR_UNSAFE(s, i, c) UTF16_PREV_CHAR_UNSAFE(s, i, c)
  862. /** @deprecated ICU 2.4. Use U16_PREV instead, see utf_old.h. */
  863. #define UTF_PREV_CHAR_SAFE(s, start, i, c, strict) UTF16_PREV_CHAR_SAFE(s, start, i, c, strict)
  864. /** @deprecated ICU 2.4. Renamed to U16_BACK_1_UNSAFE, see utf_old.h. */
  865. #define UTF_BACK_1_UNSAFE(s, i) UTF16_BACK_1_UNSAFE(s, i)
  866. /** @deprecated ICU 2.4. Renamed to U16_BACK_1, see utf_old.h. */
  867. #define UTF_BACK_1_SAFE(s, start, i) UTF16_BACK_1_SAFE(s, start, i)
  868. /** @deprecated ICU 2.4. Renamed to U16_BACK_N_UNSAFE, see utf_old.h. */
  869. #define UTF_BACK_N_UNSAFE(s, i, n) UTF16_BACK_N_UNSAFE(s, i, n)
  870. /** @deprecated ICU 2.4. Renamed to U16_BACK_N, see utf_old.h. */
  871. #define UTF_BACK_N_SAFE(s, start, i, n) UTF16_BACK_N_SAFE(s, start, i, n)
  872. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_LIMIT_UNSAFE, see utf_old.h. */
  873. #define UTF_SET_CHAR_LIMIT_UNSAFE(s, i) UTF16_SET_CHAR_LIMIT_UNSAFE(s, i)
  874. /** @deprecated ICU 2.4. Renamed to U16_SET_CP_LIMIT, see utf_old.h. */
  875. #define UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length) UTF16_SET_CHAR_LIMIT_SAFE(s, start, i, length)
  876. /* Define default macros (UTF-16 "safe") ------------------------------------ */
  877. /**
  878. * Does this code unit alone encode a code point (BMP, not a surrogate)?
  879. * Same as UTF16_IS_SINGLE.
  880. * @deprecated ICU 2.4. Renamed to U_IS_SINGLE and U16_IS_SINGLE, see utf_old.h.
  881. */
  882. #define UTF_IS_SINGLE(uchar) U16_IS_SINGLE(uchar)
  883. /**
  884. * Is this code unit the first one of several (a lead surrogate)?
  885. * Same as UTF16_IS_LEAD.
  886. * @deprecated ICU 2.4. Renamed to U_IS_LEAD and U16_IS_LEAD, see utf_old.h.
  887. */
  888. #define UTF_IS_LEAD(uchar) U16_IS_LEAD(uchar)
  889. /**
  890. * Is this code unit one of several but not the first one (a trail surrogate)?
  891. * Same as UTF16_IS_TRAIL.
  892. * @deprecated ICU 2.4. Renamed to U_IS_TRAIL and U16_IS_TRAIL, see utf_old.h.
  893. */
  894. #define UTF_IS_TRAIL(uchar) U16_IS_TRAIL(uchar)
  895. /**
  896. * Does this code point require multiple code units (is it a supplementary code point)?
  897. * Same as UTF16_NEED_MULTIPLE_UCHAR.
  898. * @deprecated ICU 2.4. Use U16_LENGTH or test ((uint32_t)(c)>0xffff) instead.
  899. */
  900. #define UTF_NEED_MULTIPLE_UCHAR(c) UTF16_NEED_MULTIPLE_UCHAR(c)
  901. /**
  902. * How many code units are used to encode this code point (1 or 2)?
  903. * Same as UTF16_CHAR_LENGTH.
  904. * @deprecated ICU 2.4. Renamed to U16_LENGTH, see utf_old.h.
  905. */
  906. #define UTF_CHAR_LENGTH(c) U16_LENGTH(c)
  907. /**
  908. * How many code units are used at most for any Unicode code point (2)?
  909. * Same as UTF16_MAX_CHAR_LENGTH.
  910. * @deprecated ICU 2.4. Renamed to U16_MAX_LENGTH, see utf_old.h.
  911. */
  912. #define UTF_MAX_CHAR_LENGTH U16_MAX_LENGTH
  913. /**
  914. * Set c to the code point that contains the code unit i.
  915. * i could point to the lead or the trail surrogate for the code point.
  916. * i is not modified.
  917. * Same as UTF16_GET_CHAR.
  918. * \pre 0<=i<length
  919. *
  920. * @deprecated ICU 2.4. Renamed to U16_GET, see utf_old.h.
  921. */
  922. #define UTF_GET_CHAR(s, start, i, length, c) U16_GET(s, start, i, length, c)
  923. /**
  924. * Set c to the code point that starts at code unit i
  925. * and advance i to beyond the code units of this code point (post-increment).
  926. * i must point to the first code unit of a code point.
  927. * Otherwise c is set to the trail unit (surrogate) itself.
  928. * Same as UTF16_NEXT_CHAR.
  929. * \pre 0<=i<length
  930. * \post 0<i<=length
  931. *
  932. * @deprecated ICU 2.4. Renamed to U16_NEXT, see utf_old.h.
  933. */
  934. #define UTF_NEXT_CHAR(s, i, length, c) U16_NEXT(s, i, length, c)
  935. /**
  936. * Append the code units of code point c to the string at index i
  937. * and advance i to beyond the new code units (post-increment).
  938. * The code units beginning at index i will be overwritten.
  939. * Same as UTF16_APPEND_CHAR.
  940. * \pre 0<=c<=0x10ffff
  941. * \pre 0<=i<length
  942. * \post 0<i<=length
  943. *
  944. * @deprecated ICU 2.4. Use U16_APPEND instead, see utf_old.h.
  945. */
  946. #define UTF_APPEND_CHAR(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
  947. /**
  948. * Advance i to beyond the code units of the code point that begins at i.
  949. * I.e., advance i by one code point.
  950. * Same as UTF16_FWD_1.
  951. * \pre 0<=i<length
  952. * \post 0<i<=length
  953. *
  954. * @deprecated ICU 2.4. Renamed to U16_FWD_1, see utf_old.h.
  955. */
  956. #define UTF_FWD_1(s, i, length) U16_FWD_1(s, i, length)
  957. /**
  958. * Advance i to beyond the code units of the n code points where the first one begins at i.
  959. * I.e., advance i by n code points.
  960. * Same as UT16_FWD_N.
  961. * \pre 0<=i<length
  962. * \post 0<i<=length
  963. *
  964. * @deprecated ICU 2.4. Renamed to U16_FWD_N, see utf_old.h.
  965. */
  966. #define UTF_FWD_N(s, i, length, n) U16_FWD_N(s, i, length, n)
  967. /**
  968. * Take the random-access index i and adjust it so that it points to the beginning
  969. * of a code point.
  970. * The input index points to any code unit of a code point and is moved to point to
  971. * the first code unit of the same code point. i is never incremented.
  972. * In other words, if i points to a trail surrogate that is preceded by a matching
  973. * lead surrogate, then i is decremented. Otherwise it is not modified.
  974. * This can be used to start an iteration with UTF_NEXT_CHAR() from a random index.
  975. * Same as UTF16_SET_CHAR_START.
  976. * \pre start<=i<length
  977. * \post start<=i<length
  978. *
  979. * @deprecated ICU 2.4. Renamed to U16_SET_CP_START, see utf_old.h.
  980. */
  981. #define UTF_SET_CHAR_START(s, start, i) U16_SET_CP_START(s, start, i)
  982. /**
  983. * Set c to the code point that has code units before i
  984. * and move i backward (towards the beginning of the string)
  985. * to the first code unit of this code point (pre-increment).
  986. * i must point to the first code unit after the last unit of a code point (i==length is allowed).
  987. * Same as UTF16_PREV_CHAR.
  988. * \pre start<i<=length
  989. * \post start<=i<length
  990. *
  991. * @deprecated ICU 2.4. Renamed to U16_PREV, see utf_old.h.
  992. */
  993. #define UTF_PREV_CHAR(s, start, i, c) U16_PREV(s, start, i, c)
  994. /**
  995. * Move i backward (towards the beginning of the string)
  996. * to the first code unit of the code point that has code units before i.
  997. * I.e., move i backward by one code point.
  998. * i must point to the first code unit after the last unit of a code point (i==length is allowed).
  999. * Same as UTF16_BACK_1.
  1000. * \pre start<i<=length
  1001. * \post start<=i<length
  1002. *
  1003. * @deprecated ICU 2.4. Renamed to U16_BACK_1, see utf_old.h.
  1004. */
  1005. #define UTF_BACK_1(s, start, i) U16_BACK_1(s, start, i)
  1006. /**
  1007. * Move i backward (towards the beginning of the string)
  1008. * to the first code unit of the n code points that have code units before i.
  1009. * I.e., move i backward by n code points.
  1010. * i must point to the first code unit after the last unit of a code point (i==length is allowed).
  1011. * Same as UTF16_BACK_N.
  1012. * \pre start<i<=length
  1013. * \post start<=i<length
  1014. *
  1015. * @deprecated ICU 2.4. Renamed to U16_BACK_N, see utf_old.h.
  1016. */
  1017. #define UTF_BACK_N(s, start, i, n) U16_BACK_N(s, start, i, n)
  1018. /**
  1019. * Take the random-access index i and adjust it so that it points beyond
  1020. * a code point. The input index points beyond any code unit
  1021. * of a code point and is moved to point beyond the last code unit of the same
  1022. * code point. i is never decremented.
  1023. * In other words, if i points to a trail surrogate that is preceded by a matching
  1024. * lead surrogate, then i is incremented. Otherwise it is not modified.
  1025. * This can be used to start an iteration with UTF_PREV_CHAR() from a random index.
  1026. * Same as UTF16_SET_CHAR_LIMIT.
  1027. * \pre start<i<=length
  1028. * \post start<i<=length
  1029. *
  1030. * @deprecated ICU 2.4. Renamed to U16_SET_CP_LIMIT, see utf_old.h.
  1031. */
  1032. #define UTF_SET_CHAR_LIMIT(s, start, i, length) U16_SET_CP_LIMIT(s, start, i, length)
  1033. #endif // !U_HIDE_DEPRECATED_API && !U_HIDE_OBSOLETE_UTF_OLD_H
  1034. #endif