uprntf_p.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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) 1998-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File uprntf_p.c
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 11/23/98 stephen Creation.
  17. * 03/12/99 stephen Modified for new C API.
  18. * 08/07/2003 george Reunify printf implementations
  19. ******************************************************************************
  20. */
  21. #include "unicode/utypes.h"
  22. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
  23. #include "unicode/ustring.h"
  24. #include "unicode/utf16.h"
  25. #include "uprintf.h"
  26. #include "ufmt_cmn.h"
  27. #include "cmemory.h"
  28. #include "putilimp.h"
  29. /* ANSI style formatting */
  30. /* Use US-ASCII characters only for formatting */
  31. /* % */
  32. #define UFMT_SIMPLE_PERCENT {ufmt_simple_percent, u_printf_simple_percent_handler}
  33. /* s */
  34. #define UFMT_STRING {ufmt_string, u_printf_string_handler}
  35. /* c */
  36. #define UFMT_CHAR {ufmt_char, u_printf_char_handler}
  37. /* d, i */
  38. #define UFMT_INT {ufmt_int, u_printf_integer_handler}
  39. /* u */
  40. #define UFMT_UINT {ufmt_int, u_printf_uinteger_handler}
  41. /* o */
  42. #define UFMT_OCTAL {ufmt_int, u_printf_octal_handler}
  43. /* x, X */
  44. #define UFMT_HEX {ufmt_int, u_printf_hex_handler}
  45. /* f */
  46. #define UFMT_DOUBLE {ufmt_double, u_printf_double_handler}
  47. /* e, E */
  48. #define UFMT_SCIENTIFIC {ufmt_double, u_printf_scientific_handler}
  49. /* g, G */
  50. #define UFMT_SCIDBL {ufmt_double, u_printf_scidbl_handler}
  51. /* n */
  52. #define UFMT_COUNT {ufmt_count, u_printf_count_handler}
  53. /* non-ANSI extensions */
  54. /* Use US-ASCII characters only for formatting */
  55. /* p */
  56. #define UFMT_POINTER {ufmt_pointer, u_printf_pointer_handler}
  57. /* V */
  58. #define UFMT_SPELLOUT {ufmt_double, u_printf_spellout_handler}
  59. /* P */
  60. #define UFMT_PERCENT {ufmt_double, u_printf_percent_handler}
  61. /* C K is old format */
  62. #define UFMT_UCHAR {ufmt_uchar, u_printf_uchar_handler}
  63. /* S U is old format */
  64. #define UFMT_USTRING {ufmt_ustring, u_printf_ustring_handler}
  65. #define UFMT_EMPTY {ufmt_empty, nullptr}
  66. /**
  67. * A u_printf handler function.
  68. * A u_printf handler is responsible for handling a single u_printf
  69. * format specification, for example 'd' or 's'.
  70. * @param stream The UFILE to which to write output.
  71. * @param info A pointer to a <TT>u_printf_spec_info</TT> struct containing
  72. * information on the format specification.
  73. * @param args A pointer to the argument data
  74. * @return The number of Unicode characters written to <TT>stream</TT>.
  75. */
  76. typedef int32_t U_EXPORT2
  77. u_printf_handler(const u_printf_stream_handler *handler,
  78. void *context,
  79. ULocaleBundle *formatBundle,
  80. const u_printf_spec_info *info,
  81. const ufmt_args *args);
  82. typedef struct u_printf_info {
  83. ufmt_type_info info;
  84. u_printf_handler *handler;
  85. } u_printf_info;
  86. /**
  87. * Struct encapsulating a single uprintf format specification.
  88. */
  89. typedef struct u_printf_spec {
  90. u_printf_spec_info fInfo; /* Information on this spec */
  91. int32_t fWidthPos; /* Position of width in arg list */
  92. int32_t fPrecisionPos; /* Position of precision in arg list */
  93. int32_t fArgPos; /* Position of data in arg list */
  94. } u_printf_spec;
  95. #define UPRINTF_NUM_FMT_HANDLERS 108
  96. /* We do not use handlers for 0-0x1f */
  97. #define UPRINTF_BASE_FMT_HANDLERS 0x20
  98. /* buffer size for formatting */
  99. #define UPRINTF_BUFFER_SIZE 1024
  100. #define UPRINTF_SYMBOL_BUFFER_SIZE 8
  101. static const char16_t gNullStr[] = {0x28, 0x6E, 0x75, 0x6C, 0x6C, 0x29, 0}; /* "(null)" */
  102. static const char16_t gSpaceStr[] = {0x20, 0}; /* " " */
  103. /* Sets the sign of a format based on u_printf_spec_info */
  104. /* TODO: Is setting the prefix symbol to a positive sign a good idea in all locales? */
  105. static void
  106. u_printf_set_sign(UNumberFormat *format,
  107. const u_printf_spec_info *info,
  108. char16_t *prefixBuffer,
  109. int32_t *prefixBufLen,
  110. UErrorCode *status)
  111. {
  112. if(info->fShowSign) {
  113. *prefixBufLen = unum_getTextAttribute(format,
  114. UNUM_POSITIVE_PREFIX,
  115. prefixBuffer,
  116. *prefixBufLen,
  117. status);
  118. if (info->fSpace) {
  119. /* Setting UNUM_PLUS_SIGN_SYMBOL affects the exponent too. */
  120. /* unum_setSymbol(format, UNUM_PLUS_SIGN_SYMBOL, gSpaceStr, 1, &status); */
  121. unum_setTextAttribute(format, UNUM_POSITIVE_PREFIX, gSpaceStr, 1, status);
  122. }
  123. else {
  124. char16_t plusSymbol[UPRINTF_SYMBOL_BUFFER_SIZE];
  125. int32_t symbolLen;
  126. symbolLen = unum_getSymbol(format,
  127. UNUM_PLUS_SIGN_SYMBOL,
  128. plusSymbol,
  129. UPRV_LENGTHOF(plusSymbol),
  130. status);
  131. unum_setTextAttribute(format,
  132. UNUM_POSITIVE_PREFIX,
  133. plusSymbol,
  134. symbolLen,
  135. status);
  136. }
  137. }
  138. else {
  139. *prefixBufLen = 0;
  140. }
  141. }
  142. static void
  143. u_printf_reset_sign(UNumberFormat *format,
  144. const u_printf_spec_info *info,
  145. char16_t *prefixBuffer,
  146. int32_t *prefixBufLen,
  147. UErrorCode *status)
  148. {
  149. if(info->fShowSign) {
  150. unum_setTextAttribute(format,
  151. UNUM_POSITIVE_PREFIX,
  152. prefixBuffer,
  153. *prefixBufLen,
  154. status);
  155. }
  156. }
  157. /* handle a '%' */
  158. static int32_t
  159. u_printf_simple_percent_handler(const u_printf_stream_handler *handler,
  160. void *context,
  161. ULocaleBundle *formatBundle,
  162. const u_printf_spec_info *info,
  163. const ufmt_args *args)
  164. {
  165. (void)formatBundle;
  166. (void)info;
  167. (void)args;
  168. static const char16_t PERCENT[] = { UP_PERCENT };
  169. /* put a single '%' onto the output */
  170. return handler->write(context, PERCENT, 1);
  171. }
  172. /* handle 's' */
  173. static int32_t
  174. u_printf_string_handler(const u_printf_stream_handler *handler,
  175. void *context,
  176. ULocaleBundle *formatBundle,
  177. const u_printf_spec_info *info,
  178. const ufmt_args *args)
  179. {
  180. (void)formatBundle;
  181. char16_t *s;
  182. char16_t buffer[UFMT_DEFAULT_BUFFER_SIZE];
  183. int32_t len, written;
  184. int32_t argSize;
  185. const char *arg = (const char*)(args[0].ptrValue);
  186. /* convert from the default codepage to Unicode */
  187. if (arg) {
  188. argSize = (int32_t)strlen(arg) + 1;
  189. if (argSize >= MAX_UCHAR_BUFFER_SIZE(buffer)) {
  190. s = ufmt_defaultCPToUnicode(arg, argSize,
  191. (char16_t *)uprv_malloc(MAX_UCHAR_BUFFER_NEEDED(argSize)),
  192. MAX_UCHAR_BUFFER_NEEDED(argSize));
  193. if(s == nullptr) {
  194. return 0;
  195. }
  196. }
  197. else {
  198. s = ufmt_defaultCPToUnicode(arg, argSize, buffer,
  199. UPRV_LENGTHOF(buffer));
  200. }
  201. }
  202. else {
  203. s = (char16_t *)gNullStr;
  204. }
  205. len = u_strlen(s);
  206. /* width = minimum # of characters to write */
  207. /* precision = maximum # of characters to write */
  208. if (info->fPrecision != -1 && info->fPrecision < len) {
  209. len = info->fPrecision;
  210. }
  211. written = handler->pad_and_justify(context, info, s, len);
  212. /* clean up */
  213. if (gNullStr != s && buffer != s) {
  214. uprv_free(s);
  215. }
  216. return written;
  217. }
  218. static int32_t
  219. u_printf_char_handler(const u_printf_stream_handler *handler,
  220. void *context,
  221. ULocaleBundle *formatBundle,
  222. const u_printf_spec_info *info,
  223. const ufmt_args *args)
  224. {
  225. (void)formatBundle;
  226. char16_t s[U16_MAX_LENGTH+1];
  227. int32_t len = 1, written;
  228. unsigned char arg = (unsigned char)(args[0].int64Value);
  229. /* convert from default codepage to Unicode */
  230. ufmt_defaultCPToUnicode((const char *)&arg, 2, s, UPRV_LENGTHOF(s));
  231. /* Remember that this may be an MBCS character */
  232. if (arg != 0) {
  233. len = u_strlen(s);
  234. }
  235. /* width = minimum # of characters to write */
  236. /* precision = maximum # of characters to write */
  237. /* precision is ignored when handling a char */
  238. written = handler->pad_and_justify(context, info, s, len);
  239. return written;
  240. }
  241. static int32_t
  242. u_printf_double_handler(const u_printf_stream_handler *handler,
  243. void *context,
  244. ULocaleBundle *formatBundle,
  245. const u_printf_spec_info *info,
  246. const ufmt_args *args)
  247. {
  248. double num = (double) (args[0].doubleValue);
  249. UNumberFormat *format;
  250. char16_t result[UPRINTF_BUFFER_SIZE];
  251. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  252. int32_t prefixBufferLen = sizeof(prefixBuffer);
  253. int32_t minDecimalDigits;
  254. int32_t maxDecimalDigits;
  255. int32_t resultLen;
  256. UErrorCode status = U_ZERO_ERROR;
  257. prefixBuffer[0] = 0;
  258. /* mask off any necessary bits */
  259. /* if(! info->fIsLongDouble)
  260. num &= DBL_MAX;*/
  261. /* get the formatter */
  262. format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
  263. /* handle error */
  264. if (format == nullptr)
  265. return 0;
  266. /* save the formatter's state */
  267. minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
  268. maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
  269. /* set the appropriate flags and number of decimal digits on the formatter */
  270. if(info->fPrecision != -1) {
  271. /* set the # of decimal digits */
  272. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  273. }
  274. else if(info->fAlt) {
  275. /* '#' means always show decimal point */
  276. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  277. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  278. }
  279. else {
  280. /* # of decimal digits is 6 if precision not specified regardless of locale */
  281. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  282. }
  283. /* set whether to show the sign */
  284. if (info->fShowSign) {
  285. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  286. }
  287. /* format the number */
  288. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  289. if (U_FAILURE(status)) {
  290. resultLen = 0;
  291. }
  292. /* restore the number format */
  293. /* TODO: Is this needed? */
  294. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  295. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  296. if (info->fShowSign) {
  297. /* Reset back to original value regardless of what the error was */
  298. UErrorCode localStatus = U_ZERO_ERROR;
  299. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  300. }
  301. return handler->pad_and_justify(context, info, result, resultLen);
  302. }
  303. /* HSYS */
  304. static int32_t
  305. u_printf_integer_handler(const u_printf_stream_handler *handler,
  306. void *context,
  307. ULocaleBundle *formatBundle,
  308. const u_printf_spec_info *info,
  309. const ufmt_args *args)
  310. {
  311. int64_t num = args[0].int64Value;
  312. UNumberFormat *format;
  313. char16_t result[UPRINTF_BUFFER_SIZE];
  314. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  315. int32_t prefixBufferLen = sizeof(prefixBuffer);
  316. int32_t minDigits = -1;
  317. int32_t resultLen;
  318. UErrorCode status = U_ZERO_ERROR;
  319. prefixBuffer[0] = 0;
  320. /* mask off any necessary bits */
  321. if (info->fIsShort)
  322. num = (int16_t)num;
  323. else if (!info->fIsLongLong)
  324. num = (int32_t)num;
  325. /* get the formatter */
  326. format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
  327. /* handle error */
  328. if (format == nullptr)
  329. return 0;
  330. /* set the appropriate flags on the formatter */
  331. /* set the minimum integer digits */
  332. if(info->fPrecision != -1) {
  333. /* set the minimum # of digits */
  334. minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS);
  335. unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision);
  336. }
  337. /* set whether to show the sign */
  338. if(info->fShowSign) {
  339. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  340. }
  341. /* format the number */
  342. resultLen = unum_formatInt64(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  343. if (U_FAILURE(status)) {
  344. resultLen = 0;
  345. }
  346. /* restore the number format */
  347. if (minDigits != -1) {
  348. unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits);
  349. }
  350. if (info->fShowSign) {
  351. /* Reset back to original value regardless of what the error was */
  352. UErrorCode localStatus = U_ZERO_ERROR;
  353. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  354. }
  355. return handler->pad_and_justify(context, info, result, resultLen);
  356. }
  357. static int32_t
  358. u_printf_hex_handler(const u_printf_stream_handler *handler,
  359. void *context,
  360. ULocaleBundle *formatBundle,
  361. const u_printf_spec_info *info,
  362. const ufmt_args *args)
  363. {
  364. (void)formatBundle;
  365. int64_t num = args[0].int64Value;
  366. char16_t result[UPRINTF_BUFFER_SIZE];
  367. int32_t len = UPRINTF_BUFFER_SIZE;
  368. /* mask off any necessary bits */
  369. if (info->fIsShort)
  370. num &= UINT16_MAX;
  371. else if (!info->fIsLongLong)
  372. num &= UINT32_MAX;
  373. /* format the number, preserving the minimum # of digits */
  374. ufmt_64tou(result, &len, num, 16,
  375. (UBool)(info->fSpec == 0x0078),
  376. (info->fPrecision == -1 && info->fZero) ? info->fWidth : info->fPrecision);
  377. /* convert to alt form, if desired */
  378. if(num != 0 && info->fAlt && len < UPRINTF_BUFFER_SIZE - 2) {
  379. /* shift the formatted string right by 2 chars */
  380. memmove(result + 2, result, len * sizeof(char16_t));
  381. result[0] = 0x0030;
  382. result[1] = info->fSpec;
  383. len += 2;
  384. }
  385. return handler->pad_and_justify(context, info, result, len);
  386. }
  387. static int32_t
  388. u_printf_octal_handler(const u_printf_stream_handler *handler,
  389. void *context,
  390. ULocaleBundle *formatBundle,
  391. const u_printf_spec_info *info,
  392. const ufmt_args *args)
  393. {
  394. (void)formatBundle;
  395. int64_t num = args[0].int64Value;
  396. char16_t result[UPRINTF_BUFFER_SIZE];
  397. int32_t len = UPRINTF_BUFFER_SIZE;
  398. /* mask off any necessary bits */
  399. if (info->fIsShort)
  400. num &= UINT16_MAX;
  401. else if (!info->fIsLongLong)
  402. num &= UINT32_MAX;
  403. /* format the number, preserving the minimum # of digits */
  404. ufmt_64tou(result, &len, num, 8,
  405. false, /* doesn't matter for octal */
  406. info->fPrecision == -1 && info->fZero ? info->fWidth : info->fPrecision);
  407. /* convert to alt form, if desired */
  408. if(info->fAlt && result[0] != 0x0030 && len < UPRINTF_BUFFER_SIZE - 1) {
  409. /* shift the formatted string right by 1 char */
  410. memmove(result + 1, result, len * sizeof(char16_t));
  411. result[0] = 0x0030;
  412. len += 1;
  413. }
  414. return handler->pad_and_justify(context, info, result, len);
  415. }
  416. static int32_t
  417. u_printf_uinteger_handler(const u_printf_stream_handler *handler,
  418. void *context,
  419. ULocaleBundle *formatBundle,
  420. const u_printf_spec_info *info,
  421. const ufmt_args *args)
  422. {
  423. int64_t num = args[0].int64Value;
  424. UNumberFormat *format;
  425. char16_t result[UPRINTF_BUFFER_SIZE];
  426. int32_t minDigits = -1;
  427. int32_t resultLen;
  428. UErrorCode status = U_ZERO_ERROR;
  429. /* TODO: Fix this once uint64_t can be formatted. */
  430. if (info->fIsShort)
  431. num &= UINT16_MAX;
  432. else if (!info->fIsLongLong)
  433. num &= UINT32_MAX;
  434. /* get the formatter */
  435. format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
  436. /* handle error */
  437. if (format == nullptr)
  438. return 0;
  439. /* set the appropriate flags on the formatter */
  440. /* set the minimum integer digits */
  441. if(info->fPrecision != -1) {
  442. /* set the minimum # of digits */
  443. minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS);
  444. unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision);
  445. }
  446. /* To mirror other stdio implementations, we ignore the sign argument */
  447. /* format the number */
  448. resultLen = unum_formatInt64(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  449. if (U_FAILURE(status)) {
  450. resultLen = 0;
  451. }
  452. /* restore the number format */
  453. if (minDigits != -1) {
  454. unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits);
  455. }
  456. return handler->pad_and_justify(context, info, result, resultLen);
  457. }
  458. static int32_t
  459. u_printf_pointer_handler(const u_printf_stream_handler *handler,
  460. void *context,
  461. ULocaleBundle *formatBundle,
  462. const u_printf_spec_info *info,
  463. const ufmt_args *args)
  464. {
  465. (void)formatBundle;
  466. char16_t result[UPRINTF_BUFFER_SIZE];
  467. int32_t len = UPRINTF_BUFFER_SIZE;
  468. /* format the pointer in hex */
  469. ufmt_ptou(result, &len, args[0].ptrValue, true/*, info->fPrecision*/);
  470. return handler->pad_and_justify(context, info, result, len);
  471. }
  472. static int32_t
  473. u_printf_scientific_handler(const u_printf_stream_handler *handler,
  474. void *context,
  475. ULocaleBundle *formatBundle,
  476. const u_printf_spec_info *info,
  477. const ufmt_args *args)
  478. {
  479. double num = (double) (args[0].doubleValue);
  480. UNumberFormat *format;
  481. char16_t result[UPRINTF_BUFFER_SIZE];
  482. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  483. int32_t prefixBufferLen = sizeof(prefixBuffer);
  484. int32_t minDecimalDigits;
  485. int32_t maxDecimalDigits;
  486. UErrorCode status = U_ZERO_ERROR;
  487. char16_t srcExpBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
  488. int32_t srcLen, expLen;
  489. int32_t resultLen;
  490. char16_t expBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
  491. prefixBuffer[0] = 0;
  492. /* mask off any necessary bits */
  493. /* if(! info->fIsLongDouble)
  494. num &= DBL_MAX;*/
  495. /* get the formatter */
  496. format = u_locbund_getNumberFormat(formatBundle, UNUM_SCIENTIFIC);
  497. /* handle error */
  498. if (format == nullptr)
  499. return 0;
  500. /* set the appropriate flags on the formatter */
  501. srcLen = unum_getSymbol(format,
  502. UNUM_EXPONENTIAL_SYMBOL,
  503. srcExpBuf,
  504. sizeof(srcExpBuf),
  505. &status);
  506. /* Upper/lower case the e */
  507. if (info->fSpec == (char16_t)0x65 /* e */) {
  508. expLen = u_strToLower(expBuf, (int32_t)sizeof(expBuf),
  509. srcExpBuf, srcLen,
  510. formatBundle->fLocale,
  511. &status);
  512. }
  513. else {
  514. expLen = u_strToUpper(expBuf, (int32_t)sizeof(expBuf),
  515. srcExpBuf, srcLen,
  516. formatBundle->fLocale,
  517. &status);
  518. }
  519. unum_setSymbol(format,
  520. UNUM_EXPONENTIAL_SYMBOL,
  521. expBuf,
  522. expLen,
  523. &status);
  524. /* save the formatter's state */
  525. minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
  526. maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
  527. /* set the appropriate flags and number of decimal digits on the formatter */
  528. if(info->fPrecision != -1) {
  529. /* set the # of decimal digits */
  530. if (info->fOrigSpec == (char16_t)0x65 /* e */ || info->fOrigSpec == (char16_t)0x45 /* E */) {
  531. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  532. }
  533. else {
  534. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, 1);
  535. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, info->fPrecision);
  536. }
  537. }
  538. else if(info->fAlt) {
  539. /* '#' means always show decimal point */
  540. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  541. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  542. }
  543. else {
  544. /* # of decimal digits is 6 if precision not specified */
  545. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  546. }
  547. /* set whether to show the sign */
  548. if (info->fShowSign) {
  549. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  550. }
  551. /* format the number */
  552. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  553. if (U_FAILURE(status)) {
  554. resultLen = 0;
  555. }
  556. /* restore the number format */
  557. /* TODO: Is this needed? */
  558. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  559. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  560. /* Since we're the only one using the scientific
  561. format, we don't need to save the old exponent value. */
  562. /*unum_setSymbol(format,
  563. UNUM_EXPONENTIAL_SYMBOL,
  564. srcExpBuf,
  565. srcLen,
  566. &status);*/
  567. if (info->fShowSign) {
  568. /* Reset back to original value regardless of what the error was */
  569. UErrorCode localStatus = U_ZERO_ERROR;
  570. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  571. }
  572. return handler->pad_and_justify(context, info, result, resultLen);
  573. }
  574. static int32_t
  575. u_printf_percent_handler(const u_printf_stream_handler *handler,
  576. void *context,
  577. ULocaleBundle *formatBundle,
  578. const u_printf_spec_info *info,
  579. const ufmt_args *args)
  580. {
  581. double num = (double) (args[0].doubleValue);
  582. UNumberFormat *format;
  583. char16_t result[UPRINTF_BUFFER_SIZE];
  584. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  585. int32_t prefixBufferLen = sizeof(prefixBuffer);
  586. int32_t minDecimalDigits;
  587. int32_t maxDecimalDigits;
  588. int32_t resultLen;
  589. UErrorCode status = U_ZERO_ERROR;
  590. prefixBuffer[0] = 0;
  591. /* mask off any necessary bits */
  592. /* if(! info->fIsLongDouble)
  593. num &= DBL_MAX;*/
  594. /* get the formatter */
  595. format = u_locbund_getNumberFormat(formatBundle, UNUM_PERCENT);
  596. /* handle error */
  597. if (format == nullptr)
  598. return 0;
  599. /* save the formatter's state */
  600. minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
  601. maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
  602. /* set the appropriate flags and number of decimal digits on the formatter */
  603. if(info->fPrecision != -1) {
  604. /* set the # of decimal digits */
  605. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  606. }
  607. else if(info->fAlt) {
  608. /* '#' means always show decimal point */
  609. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  610. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  611. }
  612. else {
  613. /* # of decimal digits is 6 if precision not specified */
  614. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  615. }
  616. /* set whether to show the sign */
  617. if (info->fShowSign) {
  618. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  619. }
  620. /* format the number */
  621. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  622. if (U_FAILURE(status)) {
  623. resultLen = 0;
  624. }
  625. /* restore the number format */
  626. /* TODO: Is this needed? */
  627. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  628. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  629. if (info->fShowSign) {
  630. /* Reset back to original value regardless of what the error was */
  631. UErrorCode localStatus = U_ZERO_ERROR;
  632. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  633. }
  634. return handler->pad_and_justify(context, info, result, resultLen);
  635. }
  636. static int32_t
  637. u_printf_ustring_handler(const u_printf_stream_handler *handler,
  638. void *context,
  639. ULocaleBundle *formatBundle,
  640. const u_printf_spec_info *info,
  641. const ufmt_args *args)
  642. {
  643. (void)formatBundle;
  644. int32_t len, written;
  645. const char16_t *arg = (const char16_t*)(args[0].ptrValue);
  646. /* allocate enough space for the buffer */
  647. if (arg == nullptr) {
  648. arg = gNullStr;
  649. }
  650. len = u_strlen(arg);
  651. /* width = minimum # of characters to write */
  652. /* precision = maximum # of characters to write */
  653. if (info->fPrecision != -1 && info->fPrecision < len) {
  654. len = info->fPrecision;
  655. }
  656. /* determine if the string should be padded */
  657. written = handler->pad_and_justify(context, info, arg, len);
  658. return written;
  659. }
  660. static int32_t
  661. u_printf_uchar_handler(const u_printf_stream_handler *handler,
  662. void *context,
  663. ULocaleBundle *formatBundle,
  664. const u_printf_spec_info *info,
  665. const ufmt_args *args)
  666. {
  667. (void)formatBundle;
  668. int32_t written = 0;
  669. char16_t arg = (char16_t)(args[0].int64Value);
  670. /* width = minimum # of characters to write */
  671. /* precision = maximum # of characters to write */
  672. /* precision is ignored when handling a uchar */
  673. /* determine if the string should be padded */
  674. written = handler->pad_and_justify(context, info, &arg, 1);
  675. return written;
  676. }
  677. static int32_t
  678. u_printf_scidbl_handler(const u_printf_stream_handler *handler,
  679. void *context,
  680. ULocaleBundle *formatBundle,
  681. const u_printf_spec_info *info,
  682. const ufmt_args *args)
  683. {
  684. u_printf_spec_info scidbl_info;
  685. double num = args[0].doubleValue;
  686. int32_t retVal;
  687. UNumberFormat *format;
  688. int32_t maxSigDecimalDigits, significantDigits;
  689. memcpy(&scidbl_info, info, sizeof(u_printf_spec_info));
  690. /* determine whether to use 'd', 'e' or 'f' notation */
  691. if (scidbl_info.fPrecision == -1 && num == uprv_trunc(num))
  692. {
  693. /* use 'f' notation */
  694. scidbl_info.fSpec = 0x0066;
  695. scidbl_info.fPrecision = 0;
  696. /* call the double handler */
  697. retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args);
  698. }
  699. else if(num < 0.0001 || (scidbl_info.fPrecision < 1 && 1000000.0 <= num)
  700. || (scidbl_info.fPrecision != -1 && num > uprv_pow10(scidbl_info.fPrecision)))
  701. {
  702. /* use 'e' or 'E' notation */
  703. scidbl_info.fSpec = scidbl_info.fSpec - 2;
  704. if (scidbl_info.fPrecision == -1) {
  705. scidbl_info.fPrecision = 5;
  706. }
  707. /* call the scientific handler */
  708. retVal = u_printf_scientific_handler(handler, context, formatBundle, &scidbl_info, args);
  709. }
  710. else {
  711. format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
  712. /* Check for null pointer */
  713. if (format == nullptr) {
  714. return 0;
  715. }
  716. maxSigDecimalDigits = unum_getAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS);
  717. significantDigits = scidbl_info.fPrecision;
  718. /* use 'f' notation */
  719. scidbl_info.fSpec = 0x0066;
  720. if (significantDigits == -1) {
  721. significantDigits = 6;
  722. }
  723. unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, true);
  724. unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, significantDigits);
  725. /* call the double handler */
  726. retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args);
  727. unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, maxSigDecimalDigits);
  728. unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, false);
  729. }
  730. return retVal;
  731. }
  732. static int32_t
  733. u_printf_count_handler(const u_printf_stream_handler *handler,
  734. void *context,
  735. ULocaleBundle *formatBundle,
  736. const u_printf_spec_info *info,
  737. const ufmt_args *args)
  738. {
  739. (void)handler;
  740. (void)context;
  741. (void)formatBundle;
  742. int32_t *count = (int32_t*)(args[0].ptrValue);
  743. /* in the special case of count, the u_printf_spec_info's width */
  744. /* will contain the # of chars written thus far */
  745. *count = info->fWidth;
  746. return 0;
  747. }
  748. static int32_t
  749. u_printf_spellout_handler(const u_printf_stream_handler *handler,
  750. void *context,
  751. ULocaleBundle *formatBundle,
  752. const u_printf_spec_info *info,
  753. const ufmt_args *args)
  754. {
  755. double num = (double) (args[0].doubleValue);
  756. UNumberFormat *format;
  757. char16_t result[UPRINTF_BUFFER_SIZE];
  758. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  759. int32_t prefixBufferLen = sizeof(prefixBuffer);
  760. int32_t minDecimalDigits;
  761. int32_t maxDecimalDigits;
  762. int32_t resultLen;
  763. UErrorCode status = U_ZERO_ERROR;
  764. prefixBuffer[0] = 0;
  765. /* mask off any necessary bits */
  766. /* if(! info->fIsLongDouble)
  767. num &= DBL_MAX;*/
  768. /* get the formatter */
  769. format = u_locbund_getNumberFormat(formatBundle, UNUM_SPELLOUT);
  770. /* handle error */
  771. if (format == nullptr)
  772. return 0;
  773. /* save the formatter's state */
  774. minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
  775. maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
  776. /* set the appropriate flags and number of decimal digits on the formatter */
  777. if(info->fPrecision != -1) {
  778. /* set the # of decimal digits */
  779. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  780. }
  781. else if(info->fAlt) {
  782. /* '#' means always show decimal point */
  783. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  784. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  785. }
  786. else {
  787. /* # of decimal digits is 6 if precision not specified */
  788. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  789. }
  790. /* set whether to show the sign */
  791. if (info->fShowSign) {
  792. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  793. }
  794. /* format the number */
  795. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  796. if (U_FAILURE(status)) {
  797. resultLen = 0;
  798. }
  799. /* restore the number format */
  800. /* TODO: Is this needed? */
  801. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  802. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  803. if (info->fShowSign) {
  804. /* Reset back to original value regardless of what the error was */
  805. UErrorCode localStatus = U_ZERO_ERROR;
  806. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  807. }
  808. return handler->pad_and_justify(context, info, result, resultLen);
  809. }
  810. /* Use US-ASCII characters only for formatting. Most codepages have
  811. characters 20-7F from Unicode. Using any other codepage specific
  812. characters will make it very difficult to format the string on
  813. non-Unicode machines */
  814. static const u_printf_info g_u_printf_infos[UPRINTF_NUM_FMT_HANDLERS] = {
  815. /* 0x20 */
  816. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  817. UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY,
  818. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  819. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  820. /* 0x30 */
  821. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  822. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  823. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  824. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  825. /* 0x40 */
  826. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR,
  827. UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL,
  828. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  829. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/,
  830. #else
  831. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  832. #endif
  833. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  834. /* 0x50 */
  835. UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING,
  836. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  837. UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY,
  838. #else
  839. UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY,
  840. #endif
  841. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  842. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  843. /* 0x60 */
  844. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR,
  845. UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL,
  846. UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY,
  847. UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL,
  848. /* 0x70 */
  849. UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING,
  850. UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY,
  851. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  852. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  853. };
  854. /* flag characters for uprintf */
  855. #define FLAG_MINUS 0x002D
  856. #define FLAG_PLUS 0x002B
  857. #define FLAG_SPACE 0x0020
  858. #define FLAG_POUND 0x0023
  859. #define FLAG_ZERO 0x0030
  860. #define FLAG_PAREN 0x0028
  861. #define ISFLAG(s) (s) == FLAG_MINUS || \
  862. (s) == FLAG_PLUS || \
  863. (s) == FLAG_SPACE || \
  864. (s) == FLAG_POUND || \
  865. (s) == FLAG_ZERO || \
  866. (s) == FLAG_PAREN
  867. /* special characters for uprintf */
  868. #define SPEC_ASTERISK 0x002A
  869. #define SPEC_DOLLARSIGN 0x0024
  870. #define SPEC_PERIOD 0x002E
  871. #define SPEC_PERCENT 0x0025
  872. /* unicode digits */
  873. #define DIGIT_ZERO 0x0030
  874. #define DIGIT_ONE 0x0031
  875. #define DIGIT_TWO 0x0032
  876. #define DIGIT_THREE 0x0033
  877. #define DIGIT_FOUR 0x0034
  878. #define DIGIT_FIVE 0x0035
  879. #define DIGIT_SIX 0x0036
  880. #define DIGIT_SEVEN 0x0037
  881. #define DIGIT_EIGHT 0x0038
  882. #define DIGIT_NINE 0x0039
  883. #define ISDIGIT(s) (s) == DIGIT_ZERO || \
  884. (s) == DIGIT_ONE || \
  885. (s) == DIGIT_TWO || \
  886. (s) == DIGIT_THREE || \
  887. (s) == DIGIT_FOUR || \
  888. (s) == DIGIT_FIVE || \
  889. (s) == DIGIT_SIX || \
  890. (s) == DIGIT_SEVEN || \
  891. (s) == DIGIT_EIGHT || \
  892. (s) == DIGIT_NINE
  893. /* u_printf modifiers */
  894. #define MOD_H 0x0068
  895. #define MOD_LOWERL 0x006C
  896. #define MOD_L 0x004C
  897. #define ISMOD(s) (s) == MOD_H || \
  898. (s) == MOD_LOWERL || \
  899. (s) == MOD_L
  900. /* Returns an array of the parsed argument type given in the format string. */
  901. static ufmt_args* parseArguments(const char16_t *alias, va_list ap, UErrorCode *status) {
  902. ufmt_args *arglist = nullptr;
  903. ufmt_type_info *typelist = nullptr;
  904. UBool *islonglong = nullptr;
  905. int32_t size = 0;
  906. int32_t pos = 0;
  907. char16_t type;
  908. uint16_t handlerNum;
  909. const char16_t *aliasStart = alias;
  910. /* get maximum number of arguments */
  911. for(;;) {
  912. /* find % */
  913. while(*alias != UP_PERCENT && *alias != 0x0000) {
  914. alias++;
  915. }
  916. if(*alias == 0x0000) {
  917. break;
  918. }
  919. alias++;
  920. /* handle the pos number */
  921. if(ISDIGIT(*alias)) {
  922. /* handle positional parameters */
  923. if(ISDIGIT(*alias)) {
  924. pos = (int) (*alias++ - DIGIT_ZERO);
  925. while(ISDIGIT(*alias)) {
  926. pos *= 10;
  927. pos += (int) (*alias++ - DIGIT_ZERO);
  928. }
  929. }
  930. /* if there is no '$', don't read anything */
  931. if(*alias != SPEC_DOLLARSIGN) {
  932. return nullptr;
  933. }
  934. } else {
  935. return nullptr;
  936. }
  937. if (pos > size) {
  938. size = pos;
  939. }
  940. }
  941. /* create the parsed argument list */
  942. typelist = (ufmt_type_info*)uprv_malloc(sizeof(ufmt_type_info) * size);
  943. islonglong = (UBool*)uprv_malloc(sizeof(UBool) * size);
  944. arglist = (ufmt_args*)uprv_malloc(sizeof(ufmt_args) * size);
  945. /* If malloc failed, return nullptr */
  946. if (!typelist || !islonglong || !arglist) {
  947. if (typelist) {
  948. uprv_free(typelist);
  949. }
  950. if (islonglong) {
  951. uprv_free(islonglong);
  952. }
  953. if (arglist) {
  954. uprv_free(arglist);
  955. }
  956. *status = U_MEMORY_ALLOCATION_ERROR;
  957. return nullptr;
  958. }
  959. /* reset alias back to the beginning */
  960. alias = aliasStart;
  961. for(;;) {
  962. /* find % */
  963. while(*alias != UP_PERCENT && *alias != 0x0000) {
  964. alias++;
  965. }
  966. if(*alias == 0x0000) {
  967. break;
  968. }
  969. alias++;
  970. /* handle positional parameters */
  971. if(ISDIGIT(*alias)) {
  972. pos = (int) (*alias++ - DIGIT_ZERO);
  973. while(ISDIGIT(*alias)) {
  974. pos *= 10;
  975. pos += (int) (*alias++ - DIGIT_ZERO);
  976. }
  977. }
  978. /* offset position by 1 */
  979. pos--;
  980. /* skip over everything except for the type */
  981. while (ISMOD(*alias) || ISFLAG(*alias) || ISDIGIT(*alias) ||
  982. *alias == SPEC_ASTERISK || *alias == SPEC_PERIOD || *alias == SPEC_DOLLARSIGN) {
  983. islonglong[pos] = false;
  984. if (ISMOD(*alias)) {
  985. alias++;
  986. if (*alias == MOD_LOWERL) {
  987. islonglong[pos] = true;
  988. }
  989. }
  990. alias++;
  991. }
  992. type = *alias;
  993. /* store the argument type in the correct position of the parsed argument list */
  994. handlerNum = (uint16_t)(type - UPRINTF_BASE_FMT_HANDLERS);
  995. if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) {
  996. typelist[pos] = g_u_printf_infos[ handlerNum ].info;
  997. } else {
  998. typelist[pos] = ufmt_empty;
  999. }
  1000. }
  1001. /* store argument in arglist */
  1002. for (pos = 0; pos < size; pos++) {
  1003. switch (typelist[pos]) {
  1004. case ufmt_string:
  1005. case ufmt_ustring:
  1006. case ufmt_pointer:
  1007. arglist[pos].ptrValue = va_arg(ap, void*);
  1008. break;
  1009. case ufmt_char:
  1010. case ufmt_uchar:
  1011. case ufmt_int:
  1012. if (islonglong[pos]) {
  1013. arglist[pos].int64Value = va_arg(ap, int64_t);
  1014. }
  1015. else {
  1016. arglist[pos].int64Value = va_arg(ap, int32_t);
  1017. }
  1018. break;
  1019. case ufmt_float:
  1020. arglist[pos].floatValue = (float) va_arg(ap, double);
  1021. break;
  1022. case ufmt_double:
  1023. arglist[pos].doubleValue = va_arg(ap, double);
  1024. break;
  1025. default:
  1026. /* else args is ignored */
  1027. arglist[pos].ptrValue = nullptr;
  1028. break;
  1029. }
  1030. }
  1031. uprv_free(typelist);
  1032. uprv_free(islonglong);
  1033. return arglist;
  1034. }
  1035. /* We parse the argument list in Unicode */
  1036. U_CFUNC int32_t
  1037. u_printf_parse(const u_printf_stream_handler *streamHandler,
  1038. const char16_t *fmt,
  1039. void *context,
  1040. u_localized_print_string *locStringContext,
  1041. ULocaleBundle *formatBundle,
  1042. int32_t *written,
  1043. va_list ap)
  1044. {
  1045. uint16_t handlerNum;
  1046. ufmt_args args;
  1047. ufmt_type_info argType;
  1048. u_printf_handler *handler;
  1049. u_printf_spec spec;
  1050. u_printf_spec_info *info = &(spec.fInfo);
  1051. const char16_t *alias = fmt;
  1052. const char16_t *backup;
  1053. const char16_t *lastAlias;
  1054. const char16_t *orgAlias = fmt;
  1055. /* parsed argument list */
  1056. ufmt_args *arglist = nullptr; /* initialized it to avoid compiler warnings */
  1057. UErrorCode status = U_ZERO_ERROR;
  1058. if (!locStringContext || locStringContext->available >= 0) {
  1059. /* get the parsed list of argument types */
  1060. arglist = parseArguments(orgAlias, ap, &status);
  1061. /* Return error if parsing failed. */
  1062. if (U_FAILURE(status)) {
  1063. return -1;
  1064. }
  1065. }
  1066. /* iterate through the pattern */
  1067. while(!locStringContext || locStringContext->available >= 0) {
  1068. /* find the next '%' */
  1069. lastAlias = alias;
  1070. while(*alias != UP_PERCENT && *alias != 0x0000) {
  1071. alias++;
  1072. }
  1073. /* write any characters before the '%' */
  1074. if(alias > lastAlias) {
  1075. *written += (streamHandler->write)(context, lastAlias, (int32_t)(alias - lastAlias));
  1076. }
  1077. /* break if at end of string */
  1078. if(*alias == 0x0000) {
  1079. break;
  1080. }
  1081. /* initialize spec to default values */
  1082. spec.fWidthPos = -1;
  1083. spec.fPrecisionPos = -1;
  1084. spec.fArgPos = -1;
  1085. uprv_memset(info, 0, sizeof(*info));
  1086. info->fPrecision = -1;
  1087. info->fWidth = -1;
  1088. info->fPadChar = 0x0020;
  1089. /* skip over the initial '%' */
  1090. alias++;
  1091. /* Check for positional argument */
  1092. if(ISDIGIT(*alias)) {
  1093. /* Save the current position */
  1094. backup = alias;
  1095. /* handle positional parameters */
  1096. if(ISDIGIT(*alias)) {
  1097. spec.fArgPos = (int) (*alias++ - DIGIT_ZERO);
  1098. while(ISDIGIT(*alias)) {
  1099. spec.fArgPos *= 10;
  1100. spec.fArgPos += (int) (*alias++ - DIGIT_ZERO);
  1101. }
  1102. }
  1103. /* if there is no '$', don't read anything */
  1104. if(*alias != SPEC_DOLLARSIGN) {
  1105. spec.fArgPos = -1;
  1106. alias = backup;
  1107. }
  1108. /* munge the '$' */
  1109. else
  1110. alias++;
  1111. }
  1112. /* Get any format flags */
  1113. while(ISFLAG(*alias)) {
  1114. switch(*alias++) {
  1115. /* left justify */
  1116. case FLAG_MINUS:
  1117. info->fLeft = true;
  1118. break;
  1119. /* always show sign */
  1120. case FLAG_PLUS:
  1121. info->fShowSign = true;
  1122. break;
  1123. /* use space if no sign present */
  1124. case FLAG_SPACE:
  1125. info->fShowSign = true;
  1126. info->fSpace = true;
  1127. break;
  1128. /* use alternate form */
  1129. case FLAG_POUND:
  1130. info->fAlt = true;
  1131. break;
  1132. /* pad with leading zeroes */
  1133. case FLAG_ZERO:
  1134. info->fZero = true;
  1135. info->fPadChar = 0x0030;
  1136. break;
  1137. /* pad character specified */
  1138. case FLAG_PAREN:
  1139. /* TODO test that all four are numbers */
  1140. /* first four characters are hex values for pad char */
  1141. info->fPadChar = (char16_t)ufmt_digitvalue(*alias++);
  1142. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
  1143. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
  1144. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
  1145. /* final character is ignored */
  1146. alias++;
  1147. break;
  1148. }
  1149. }
  1150. /* Get the width */
  1151. /* width is specified out of line */
  1152. if(*alias == SPEC_ASTERISK) {
  1153. info->fWidth = -2;
  1154. /* Skip the '*' */
  1155. alias++;
  1156. /* Save the current position */
  1157. backup = alias;
  1158. /* handle positional parameters */
  1159. if(ISDIGIT(*alias)) {
  1160. spec.fWidthPos = (int) (*alias++ - DIGIT_ZERO);
  1161. while(ISDIGIT(*alias)) {
  1162. spec.fWidthPos *= 10;
  1163. spec.fWidthPos += (int) (*alias++ - DIGIT_ZERO);
  1164. }
  1165. }
  1166. /* if there is no '$', don't read anything */
  1167. if(*alias != SPEC_DOLLARSIGN) {
  1168. spec.fWidthPos = -1;
  1169. alias = backup;
  1170. }
  1171. /* munge the '$' */
  1172. else
  1173. alias++;
  1174. }
  1175. /* read the width, if present */
  1176. else if(ISDIGIT(*alias)){
  1177. info->fWidth = (int) (*alias++ - DIGIT_ZERO);
  1178. while(ISDIGIT(*alias)) {
  1179. info->fWidth *= 10;
  1180. info->fWidth += (int) (*alias++ - DIGIT_ZERO);
  1181. }
  1182. }
  1183. /* Get the precision */
  1184. if(*alias == SPEC_PERIOD) {
  1185. /* eat up the '.' */
  1186. alias++;
  1187. /* precision is specified out of line */
  1188. if(*alias == SPEC_ASTERISK) {
  1189. info->fPrecision = -2;
  1190. /* Skip the '*' */
  1191. alias++;
  1192. /* save the current position */
  1193. backup = alias;
  1194. /* handle positional parameters */
  1195. if(ISDIGIT(*alias)) {
  1196. spec.fPrecisionPos = (int) (*alias++ - DIGIT_ZERO);
  1197. while(ISDIGIT(*alias)) {
  1198. spec.fPrecisionPos *= 10;
  1199. spec.fPrecisionPos += (int) (*alias++ - DIGIT_ZERO);
  1200. }
  1201. /* if there is no '$', don't read anything */
  1202. if(*alias != SPEC_DOLLARSIGN) {
  1203. spec.fPrecisionPos = -1;
  1204. alias = backup;
  1205. }
  1206. else {
  1207. /* munge the '$' */
  1208. alias++;
  1209. }
  1210. }
  1211. }
  1212. /* read the precision */
  1213. else if(ISDIGIT(*alias)){
  1214. info->fPrecision = (int) (*alias++ - DIGIT_ZERO);
  1215. while(ISDIGIT(*alias)) {
  1216. info->fPrecision *= 10;
  1217. info->fPrecision += (int) (*alias++ - DIGIT_ZERO);
  1218. }
  1219. }
  1220. }
  1221. /* Get any modifiers */
  1222. if(ISMOD(*alias)) {
  1223. switch(*alias++) {
  1224. /* short */
  1225. case MOD_H:
  1226. info->fIsShort = true;
  1227. break;
  1228. /* long or long long */
  1229. case MOD_LOWERL:
  1230. if(*alias == MOD_LOWERL) {
  1231. info->fIsLongLong = true;
  1232. /* skip over the next 'l' */
  1233. alias++;
  1234. }
  1235. else
  1236. info->fIsLong = true;
  1237. break;
  1238. /* long double */
  1239. case MOD_L:
  1240. info->fIsLongDouble = true;
  1241. break;
  1242. }
  1243. }
  1244. /* finally, get the specifier letter */
  1245. info->fSpec = *alias++;
  1246. info->fOrigSpec = info->fSpec;
  1247. /* fill in the precision and width, if specified out of line */
  1248. /* width specified out of line */
  1249. if(spec.fInfo.fWidth == -2) {
  1250. if(spec.fWidthPos == -1) {
  1251. /* read the width from the argument list */
  1252. info->fWidth = va_arg(ap, int32_t);
  1253. }
  1254. /* else handle positional parameter */
  1255. /* if it's negative, take the absolute value and set left alignment */
  1256. if(info->fWidth < 0) {
  1257. info->fWidth *= -1; /* Make positive */
  1258. info->fLeft = true;
  1259. }
  1260. }
  1261. /* precision specified out of line */
  1262. if(info->fPrecision == -2) {
  1263. if(spec.fPrecisionPos == -1) {
  1264. /* read the precision from the argument list */
  1265. info->fPrecision = va_arg(ap, int32_t);
  1266. }
  1267. /* else handle positional parameter */
  1268. /* if it's negative, set it to zero */
  1269. if(info->fPrecision < 0)
  1270. info->fPrecision = 0;
  1271. }
  1272. handlerNum = (uint16_t)(info->fSpec - UPRINTF_BASE_FMT_HANDLERS);
  1273. if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) {
  1274. /* query the info function for argument information */
  1275. argType = g_u_printf_infos[ handlerNum ].info;
  1276. /* goto the correct argument on arg_list if position is specified */
  1277. if (spec.fArgPos > 0) {
  1278. /* offset position by 1 */
  1279. spec.fArgPos--;
  1280. switch(argType) {
  1281. case ufmt_count:
  1282. /* set the spec's width to the # of chars written */
  1283. info->fWidth = *written;
  1284. /* fall through to set the pointer */
  1285. U_FALLTHROUGH;
  1286. case ufmt_string:
  1287. case ufmt_ustring:
  1288. case ufmt_pointer:
  1289. args.ptrValue = arglist[spec.fArgPos].ptrValue;
  1290. break;
  1291. case ufmt_char:
  1292. case ufmt_uchar:
  1293. case ufmt_int:
  1294. args.int64Value = arglist[spec.fArgPos].int64Value;
  1295. break;
  1296. case ufmt_float:
  1297. args.floatValue = arglist[spec.fArgPos].floatValue;
  1298. break;
  1299. case ufmt_double:
  1300. args.doubleValue = arglist[spec.fArgPos].doubleValue;
  1301. break;
  1302. default:
  1303. /* else args is ignored */
  1304. args.ptrValue = nullptr;
  1305. break;
  1306. }
  1307. } else { /* no positional argument specified */
  1308. switch(argType) {
  1309. case ufmt_count:
  1310. /* set the spec's width to the # of chars written */
  1311. info->fWidth = *written;
  1312. /* fall through to set the pointer */
  1313. U_FALLTHROUGH;
  1314. case ufmt_string:
  1315. case ufmt_ustring:
  1316. case ufmt_pointer:
  1317. args.ptrValue = va_arg(ap, void*);
  1318. break;
  1319. case ufmt_char:
  1320. case ufmt_uchar:
  1321. case ufmt_int:
  1322. if (info->fIsLongLong) {
  1323. args.int64Value = va_arg(ap, int64_t);
  1324. }
  1325. else {
  1326. args.int64Value = va_arg(ap, int32_t);
  1327. }
  1328. break;
  1329. case ufmt_float:
  1330. args.floatValue = (float) va_arg(ap, double);
  1331. break;
  1332. case ufmt_double:
  1333. args.doubleValue = va_arg(ap, double);
  1334. break;
  1335. default:
  1336. /* else args is ignored */
  1337. args.ptrValue = nullptr;
  1338. break;
  1339. }
  1340. }
  1341. /* call the handler function */
  1342. handler = g_u_printf_infos[ handlerNum ].handler;
  1343. if (handler != nullptr) {
  1344. *written += (*handler)(streamHandler, context, formatBundle, info, &args);
  1345. }
  1346. else {
  1347. /* just echo unknown tags */
  1348. *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias));
  1349. }
  1350. }
  1351. else {
  1352. /* just echo unknown tags */
  1353. *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias));
  1354. }
  1355. }
  1356. /* delete parsed argument list */
  1357. if (arglist != nullptr) {
  1358. uprv_free(arglist);
  1359. }
  1360. /* return # of characters in this format that have been parsed. */
  1361. return (int32_t)(alias - fmt);
  1362. }
  1363. #endif /* #if !UCONFIG_NO_FORMATTING */