uprntf_p.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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 = static_cast<const char*>(args[0].ptrValue);
  186. /* convert from the default codepage to Unicode */
  187. if (arg) {
  188. argSize = static_cast<int32_t>(strlen(arg)) + 1;
  189. if (argSize >= MAX_UCHAR_BUFFER_SIZE(buffer)) {
  190. s = ufmt_defaultCPToUnicode(arg, argSize,
  191. static_cast<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 = static_cast<unsigned char>(args[0].int64Value);
  229. /* convert from default codepage to Unicode */
  230. ufmt_defaultCPToUnicode(reinterpret_cast<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 = 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 = static_cast<int16_t>(num);
  323. else if (!info->fIsLongLong)
  324. num = static_cast<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. static_cast<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 = 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 == static_cast<char16_t>(0x65) /* e */) {
  508. expLen = u_strToLower(expBuf, static_cast<int32_t>(sizeof(expBuf)),
  509. srcExpBuf, srcLen,
  510. formatBundle->fLocale,
  511. &status);
  512. }
  513. else {
  514. expLen = u_strToUpper(expBuf, static_cast<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 == static_cast<char16_t>(0x65) /* e */ ||
  531. info->fOrigSpec == static_cast<char16_t>(0x45) /* E */) {
  532. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  533. }
  534. else {
  535. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, 1);
  536. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, info->fPrecision);
  537. }
  538. }
  539. else if(info->fAlt) {
  540. /* '#' means always show decimal point */
  541. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  542. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  543. }
  544. else {
  545. /* # of decimal digits is 6 if precision not specified */
  546. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  547. }
  548. /* set whether to show the sign */
  549. if (info->fShowSign) {
  550. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  551. }
  552. /* format the number */
  553. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  554. if (U_FAILURE(status)) {
  555. resultLen = 0;
  556. }
  557. /* restore the number format */
  558. /* TODO: Is this needed? */
  559. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  560. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  561. /* Since we're the only one using the scientific
  562. format, we don't need to save the old exponent value. */
  563. /*unum_setSymbol(format,
  564. UNUM_EXPONENTIAL_SYMBOL,
  565. srcExpBuf,
  566. srcLen,
  567. &status);*/
  568. if (info->fShowSign) {
  569. /* Reset back to original value regardless of what the error was */
  570. UErrorCode localStatus = U_ZERO_ERROR;
  571. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  572. }
  573. return handler->pad_and_justify(context, info, result, resultLen);
  574. }
  575. static int32_t
  576. u_printf_percent_handler(const u_printf_stream_handler *handler,
  577. void *context,
  578. ULocaleBundle *formatBundle,
  579. const u_printf_spec_info *info,
  580. const ufmt_args *args)
  581. {
  582. double num = args[0].doubleValue;
  583. UNumberFormat *format;
  584. char16_t result[UPRINTF_BUFFER_SIZE];
  585. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  586. int32_t prefixBufferLen = sizeof(prefixBuffer);
  587. int32_t minDecimalDigits;
  588. int32_t maxDecimalDigits;
  589. int32_t resultLen;
  590. UErrorCode status = U_ZERO_ERROR;
  591. prefixBuffer[0] = 0;
  592. /* mask off any necessary bits */
  593. /* if(! info->fIsLongDouble)
  594. num &= DBL_MAX;*/
  595. /* get the formatter */
  596. format = u_locbund_getNumberFormat(formatBundle, UNUM_PERCENT);
  597. /* handle error */
  598. if (format == nullptr)
  599. return 0;
  600. /* save the formatter's state */
  601. minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
  602. maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
  603. /* set the appropriate flags and number of decimal digits on the formatter */
  604. if(info->fPrecision != -1) {
  605. /* set the # of decimal digits */
  606. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  607. }
  608. else if(info->fAlt) {
  609. /* '#' means always show decimal point */
  610. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  611. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  612. }
  613. else {
  614. /* # of decimal digits is 6 if precision not specified */
  615. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  616. }
  617. /* set whether to show the sign */
  618. if (info->fShowSign) {
  619. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  620. }
  621. /* format the number */
  622. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  623. if (U_FAILURE(status)) {
  624. resultLen = 0;
  625. }
  626. /* restore the number format */
  627. /* TODO: Is this needed? */
  628. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  629. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  630. if (info->fShowSign) {
  631. /* Reset back to original value regardless of what the error was */
  632. UErrorCode localStatus = U_ZERO_ERROR;
  633. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  634. }
  635. return handler->pad_and_justify(context, info, result, resultLen);
  636. }
  637. static int32_t
  638. u_printf_ustring_handler(const u_printf_stream_handler *handler,
  639. void *context,
  640. ULocaleBundle *formatBundle,
  641. const u_printf_spec_info *info,
  642. const ufmt_args *args)
  643. {
  644. (void)formatBundle;
  645. int32_t len, written;
  646. const char16_t* arg = static_cast<const char16_t*>(args[0].ptrValue);
  647. /* allocate enough space for the buffer */
  648. if (arg == nullptr) {
  649. arg = gNullStr;
  650. }
  651. len = u_strlen(arg);
  652. /* width = minimum # of characters to write */
  653. /* precision = maximum # of characters to write */
  654. if (info->fPrecision != -1 && info->fPrecision < len) {
  655. len = info->fPrecision;
  656. }
  657. /* determine if the string should be padded */
  658. written = handler->pad_and_justify(context, info, arg, len);
  659. return written;
  660. }
  661. static int32_t
  662. u_printf_uchar_handler(const u_printf_stream_handler *handler,
  663. void *context,
  664. ULocaleBundle *formatBundle,
  665. const u_printf_spec_info *info,
  666. const ufmt_args *args)
  667. {
  668. (void)formatBundle;
  669. int32_t written = 0;
  670. char16_t arg = static_cast<char16_t>(args[0].int64Value);
  671. /* width = minimum # of characters to write */
  672. /* precision = maximum # of characters to write */
  673. /* precision is ignored when handling a uchar */
  674. /* determine if the string should be padded */
  675. written = handler->pad_and_justify(context, info, &arg, 1);
  676. return written;
  677. }
  678. static int32_t
  679. u_printf_scidbl_handler(const u_printf_stream_handler *handler,
  680. void *context,
  681. ULocaleBundle *formatBundle,
  682. const u_printf_spec_info *info,
  683. const ufmt_args *args)
  684. {
  685. u_printf_spec_info scidbl_info;
  686. double num = args[0].doubleValue;
  687. int32_t retVal;
  688. UNumberFormat *format;
  689. int32_t maxSigDecimalDigits, significantDigits;
  690. memcpy(&scidbl_info, info, sizeof(u_printf_spec_info));
  691. /* determine whether to use 'd', 'e' or 'f' notation */
  692. if (scidbl_info.fPrecision == -1 && num == uprv_trunc(num))
  693. {
  694. /* use 'f' notation */
  695. scidbl_info.fSpec = 0x0066;
  696. scidbl_info.fPrecision = 0;
  697. /* call the double handler */
  698. retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args);
  699. }
  700. else if(num < 0.0001 || (scidbl_info.fPrecision < 1 && 1000000.0 <= num)
  701. || (scidbl_info.fPrecision != -1 && num > uprv_pow10(scidbl_info.fPrecision)))
  702. {
  703. /* use 'e' or 'E' notation */
  704. scidbl_info.fSpec = scidbl_info.fSpec - 2;
  705. if (scidbl_info.fPrecision == -1) {
  706. scidbl_info.fPrecision = 5;
  707. }
  708. /* call the scientific handler */
  709. retVal = u_printf_scientific_handler(handler, context, formatBundle, &scidbl_info, args);
  710. }
  711. else {
  712. format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
  713. /* Check for null pointer */
  714. if (format == nullptr) {
  715. return 0;
  716. }
  717. maxSigDecimalDigits = unum_getAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS);
  718. significantDigits = scidbl_info.fPrecision;
  719. /* use 'f' notation */
  720. scidbl_info.fSpec = 0x0066;
  721. if (significantDigits == -1) {
  722. significantDigits = 6;
  723. }
  724. unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, true);
  725. unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, significantDigits);
  726. /* call the double handler */
  727. retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args);
  728. unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, maxSigDecimalDigits);
  729. unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, false);
  730. }
  731. return retVal;
  732. }
  733. static int32_t
  734. u_printf_count_handler(const u_printf_stream_handler *handler,
  735. void *context,
  736. ULocaleBundle *formatBundle,
  737. const u_printf_spec_info *info,
  738. const ufmt_args *args)
  739. {
  740. (void)handler;
  741. (void)context;
  742. (void)formatBundle;
  743. int32_t* count = static_cast<int32_t*>(args[0].ptrValue);
  744. /* in the special case of count, the u_printf_spec_info's width */
  745. /* will contain the # of chars written thus far */
  746. *count = info->fWidth;
  747. return 0;
  748. }
  749. static int32_t
  750. u_printf_spellout_handler(const u_printf_stream_handler *handler,
  751. void *context,
  752. ULocaleBundle *formatBundle,
  753. const u_printf_spec_info *info,
  754. const ufmt_args *args)
  755. {
  756. double num = args[0].doubleValue;
  757. UNumberFormat *format;
  758. char16_t result[UPRINTF_BUFFER_SIZE];
  759. char16_t prefixBuffer[UPRINTF_BUFFER_SIZE];
  760. int32_t prefixBufferLen = sizeof(prefixBuffer);
  761. int32_t minDecimalDigits;
  762. int32_t maxDecimalDigits;
  763. int32_t resultLen;
  764. UErrorCode status = U_ZERO_ERROR;
  765. prefixBuffer[0] = 0;
  766. /* mask off any necessary bits */
  767. /* if(! info->fIsLongDouble)
  768. num &= DBL_MAX;*/
  769. /* get the formatter */
  770. format = u_locbund_getNumberFormat(formatBundle, UNUM_SPELLOUT);
  771. /* handle error */
  772. if (format == nullptr)
  773. return 0;
  774. /* save the formatter's state */
  775. minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
  776. maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
  777. /* set the appropriate flags and number of decimal digits on the formatter */
  778. if(info->fPrecision != -1) {
  779. /* set the # of decimal digits */
  780. unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
  781. }
  782. else if(info->fAlt) {
  783. /* '#' means always show decimal point */
  784. /* copy of printf behavior on Solaris - '#' shows 6 digits */
  785. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  786. }
  787. else {
  788. /* # of decimal digits is 6 if precision not specified */
  789. unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
  790. }
  791. /* set whether to show the sign */
  792. if (info->fShowSign) {
  793. u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
  794. }
  795. /* format the number */
  796. resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, nullptr, &status);
  797. if (U_FAILURE(status)) {
  798. resultLen = 0;
  799. }
  800. /* restore the number format */
  801. /* TODO: Is this needed? */
  802. unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
  803. unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
  804. if (info->fShowSign) {
  805. /* Reset back to original value regardless of what the error was */
  806. UErrorCode localStatus = U_ZERO_ERROR;
  807. u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
  808. }
  809. return handler->pad_and_justify(context, info, result, resultLen);
  810. }
  811. /* Use US-ASCII characters only for formatting. Most codepages have
  812. characters 20-7F from Unicode. Using any other codepage specific
  813. characters will make it very difficult to format the string on
  814. non-Unicode machines */
  815. static const u_printf_info g_u_printf_infos[UPRINTF_NUM_FMT_HANDLERS] = {
  816. /* 0x20 */
  817. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  818. UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY,
  819. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  820. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  821. /* 0x30 */
  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. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  826. /* 0x40 */
  827. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR,
  828. UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL,
  829. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  830. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/,
  831. #else
  832. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  833. #endif
  834. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  835. /* 0x50 */
  836. UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING,
  837. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  838. UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY,
  839. #else
  840. UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY,
  841. #endif
  842. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  843. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  844. /* 0x60 */
  845. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR,
  846. UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL,
  847. UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY,
  848. UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL,
  849. /* 0x70 */
  850. UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING,
  851. UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY,
  852. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  853. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  854. };
  855. /* flag characters for uprintf */
  856. #define FLAG_MINUS 0x002D
  857. #define FLAG_PLUS 0x002B
  858. #define FLAG_SPACE 0x0020
  859. #define FLAG_POUND 0x0023
  860. #define FLAG_ZERO 0x0030
  861. #define FLAG_PAREN 0x0028
  862. #define ISFLAG(s) (s) == FLAG_MINUS || \
  863. (s) == FLAG_PLUS || \
  864. (s) == FLAG_SPACE || \
  865. (s) == FLAG_POUND || \
  866. (s) == FLAG_ZERO || \
  867. (s) == FLAG_PAREN
  868. /* special characters for uprintf */
  869. #define SPEC_ASTERISK 0x002A
  870. #define SPEC_DOLLARSIGN 0x0024
  871. #define SPEC_PERIOD 0x002E
  872. #define SPEC_PERCENT 0x0025
  873. /* unicode digits */
  874. #define DIGIT_ZERO 0x0030
  875. #define DIGIT_ONE 0x0031
  876. #define DIGIT_TWO 0x0032
  877. #define DIGIT_THREE 0x0033
  878. #define DIGIT_FOUR 0x0034
  879. #define DIGIT_FIVE 0x0035
  880. #define DIGIT_SIX 0x0036
  881. #define DIGIT_SEVEN 0x0037
  882. #define DIGIT_EIGHT 0x0038
  883. #define DIGIT_NINE 0x0039
  884. #define ISDIGIT(s) (s) == DIGIT_ZERO || \
  885. (s) == DIGIT_ONE || \
  886. (s) == DIGIT_TWO || \
  887. (s) == DIGIT_THREE || \
  888. (s) == DIGIT_FOUR || \
  889. (s) == DIGIT_FIVE || \
  890. (s) == DIGIT_SIX || \
  891. (s) == DIGIT_SEVEN || \
  892. (s) == DIGIT_EIGHT || \
  893. (s) == DIGIT_NINE
  894. /* u_printf modifiers */
  895. #define MOD_H 0x0068
  896. #define MOD_LOWERL 0x006C
  897. #define MOD_L 0x004C
  898. #define ISMOD(s) (s) == MOD_H || \
  899. (s) == MOD_LOWERL || \
  900. (s) == MOD_L
  901. /* Returns an array of the parsed argument type given in the format string. */
  902. static ufmt_args* parseArguments(const char16_t *alias, va_list ap, UErrorCode *status) {
  903. ufmt_args *arglist = nullptr;
  904. ufmt_type_info *typelist = nullptr;
  905. UBool *islonglong = nullptr;
  906. int32_t size = 0;
  907. int32_t pos = 0;
  908. char16_t type;
  909. uint16_t handlerNum;
  910. const char16_t *aliasStart = alias;
  911. /* get maximum number of arguments */
  912. for(;;) {
  913. /* find % */
  914. while(*alias != UP_PERCENT && *alias != 0x0000) {
  915. alias++;
  916. }
  917. if(*alias == 0x0000) {
  918. break;
  919. }
  920. alias++;
  921. /* handle the pos number */
  922. if(ISDIGIT(*alias)) {
  923. /* handle positional parameters */
  924. if(ISDIGIT(*alias)) {
  925. pos = *alias++ - DIGIT_ZERO;
  926. while(ISDIGIT(*alias)) {
  927. pos *= 10;
  928. pos += *alias++ - DIGIT_ZERO;
  929. }
  930. }
  931. /* if there is no '$', don't read anything */
  932. if(*alias != SPEC_DOLLARSIGN) {
  933. return nullptr;
  934. }
  935. } else {
  936. return nullptr;
  937. }
  938. if (pos > size) {
  939. size = pos;
  940. }
  941. }
  942. /* create the parsed argument list */
  943. typelist = static_cast<ufmt_type_info*>(uprv_malloc(sizeof(ufmt_type_info) * size));
  944. islonglong = static_cast<UBool*>(uprv_malloc(sizeof(UBool) * size));
  945. arglist = static_cast<ufmt_args*>(uprv_malloc(sizeof(ufmt_args) * size));
  946. /* If malloc failed, return nullptr */
  947. if (!typelist || !islonglong || !arglist) {
  948. if (typelist) {
  949. uprv_free(typelist);
  950. }
  951. if (islonglong) {
  952. uprv_free(islonglong);
  953. }
  954. if (arglist) {
  955. uprv_free(arglist);
  956. }
  957. *status = U_MEMORY_ALLOCATION_ERROR;
  958. return nullptr;
  959. }
  960. /* reset alias back to the beginning */
  961. alias = aliasStart;
  962. for(;;) {
  963. /* find % */
  964. while(*alias != UP_PERCENT && *alias != 0x0000) {
  965. alias++;
  966. }
  967. if(*alias == 0x0000) {
  968. break;
  969. }
  970. alias++;
  971. /* handle positional parameters */
  972. if(ISDIGIT(*alias)) {
  973. pos = *alias++ - DIGIT_ZERO;
  974. while(ISDIGIT(*alias)) {
  975. pos *= 10;
  976. pos += *alias++ - DIGIT_ZERO;
  977. }
  978. }
  979. /* offset position by 1 */
  980. pos--;
  981. /* skip over everything except for the type */
  982. while (ISMOD(*alias) || ISFLAG(*alias) || ISDIGIT(*alias) ||
  983. *alias == SPEC_ASTERISK || *alias == SPEC_PERIOD || *alias == SPEC_DOLLARSIGN) {
  984. islonglong[pos] = false;
  985. if (ISMOD(*alias)) {
  986. alias++;
  987. if (*alias == MOD_LOWERL) {
  988. islonglong[pos] = true;
  989. }
  990. }
  991. alias++;
  992. }
  993. type = *alias;
  994. /* store the argument type in the correct position of the parsed argument list */
  995. handlerNum = static_cast<uint16_t>(type - UPRINTF_BASE_FMT_HANDLERS);
  996. if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) {
  997. typelist[pos] = g_u_printf_infos[ handlerNum ].info;
  998. } else {
  999. typelist[pos] = ufmt_empty;
  1000. }
  1001. }
  1002. /* store argument in arglist */
  1003. for (pos = 0; pos < size; pos++) {
  1004. switch (typelist[pos]) {
  1005. case ufmt_string:
  1006. case ufmt_ustring:
  1007. case ufmt_pointer:
  1008. arglist[pos].ptrValue = va_arg(ap, void*);
  1009. break;
  1010. case ufmt_char:
  1011. case ufmt_uchar:
  1012. case ufmt_int:
  1013. if (islonglong[pos]) {
  1014. arglist[pos].int64Value = va_arg(ap, int64_t);
  1015. }
  1016. else {
  1017. arglist[pos].int64Value = va_arg(ap, int32_t);
  1018. }
  1019. break;
  1020. case ufmt_float:
  1021. arglist[pos].floatValue = static_cast<float>(va_arg(ap, double));
  1022. break;
  1023. case ufmt_double:
  1024. arglist[pos].doubleValue = va_arg(ap, double);
  1025. break;
  1026. default:
  1027. /* else args is ignored */
  1028. arglist[pos].ptrValue = nullptr;
  1029. break;
  1030. }
  1031. }
  1032. uprv_free(typelist);
  1033. uprv_free(islonglong);
  1034. return arglist;
  1035. }
  1036. /* We parse the argument list in Unicode */
  1037. U_CFUNC int32_t
  1038. u_printf_parse(const u_printf_stream_handler *streamHandler,
  1039. const char16_t *fmt,
  1040. void *context,
  1041. u_localized_print_string *locStringContext,
  1042. ULocaleBundle *formatBundle,
  1043. int32_t *written,
  1044. va_list ap)
  1045. {
  1046. uint16_t handlerNum;
  1047. ufmt_args args;
  1048. ufmt_type_info argType;
  1049. u_printf_handler *handler;
  1050. u_printf_spec spec;
  1051. u_printf_spec_info *info = &(spec.fInfo);
  1052. const char16_t *alias = fmt;
  1053. const char16_t *backup;
  1054. const char16_t *lastAlias;
  1055. const char16_t *orgAlias = fmt;
  1056. /* parsed argument list */
  1057. ufmt_args *arglist = nullptr; /* initialized it to avoid compiler warnings */
  1058. UErrorCode status = U_ZERO_ERROR;
  1059. if (!locStringContext || locStringContext->available >= 0) {
  1060. /* get the parsed list of argument types */
  1061. arglist = parseArguments(orgAlias, ap, &status);
  1062. /* Return error if parsing failed. */
  1063. if (U_FAILURE(status)) {
  1064. return -1;
  1065. }
  1066. }
  1067. /* iterate through the pattern */
  1068. while(!locStringContext || locStringContext->available >= 0) {
  1069. /* find the next '%' */
  1070. lastAlias = alias;
  1071. while(*alias != UP_PERCENT && *alias != 0x0000) {
  1072. alias++;
  1073. }
  1074. /* write any characters before the '%' */
  1075. if(alias > lastAlias) {
  1076. *written += (streamHandler->write)(context, lastAlias, (int32_t)(alias - lastAlias));
  1077. }
  1078. /* break if at end of string */
  1079. if(*alias == 0x0000) {
  1080. break;
  1081. }
  1082. /* initialize spec to default values */
  1083. spec.fWidthPos = -1;
  1084. spec.fPrecisionPos = -1;
  1085. spec.fArgPos = -1;
  1086. uprv_memset(info, 0, sizeof(*info));
  1087. info->fPrecision = -1;
  1088. info->fWidth = -1;
  1089. info->fPadChar = 0x0020;
  1090. /* skip over the initial '%' */
  1091. alias++;
  1092. /* Check for positional argument */
  1093. if(ISDIGIT(*alias)) {
  1094. /* Save the current position */
  1095. backup = alias;
  1096. /* handle positional parameters */
  1097. if(ISDIGIT(*alias)) {
  1098. spec.fArgPos = *alias++ - DIGIT_ZERO;
  1099. while(ISDIGIT(*alias)) {
  1100. spec.fArgPos *= 10;
  1101. spec.fArgPos += *alias++ - DIGIT_ZERO;
  1102. }
  1103. }
  1104. /* if there is no '$', don't read anything */
  1105. if(*alias != SPEC_DOLLARSIGN) {
  1106. spec.fArgPos = -1;
  1107. alias = backup;
  1108. }
  1109. /* munge the '$' */
  1110. else
  1111. alias++;
  1112. }
  1113. /* Get any format flags */
  1114. while(ISFLAG(*alias)) {
  1115. switch(*alias++) {
  1116. /* left justify */
  1117. case FLAG_MINUS:
  1118. info->fLeft = true;
  1119. break;
  1120. /* always show sign */
  1121. case FLAG_PLUS:
  1122. info->fShowSign = true;
  1123. break;
  1124. /* use space if no sign present */
  1125. case FLAG_SPACE:
  1126. info->fShowSign = true;
  1127. info->fSpace = true;
  1128. break;
  1129. /* use alternate form */
  1130. case FLAG_POUND:
  1131. info->fAlt = true;
  1132. break;
  1133. /* pad with leading zeroes */
  1134. case FLAG_ZERO:
  1135. info->fZero = true;
  1136. info->fPadChar = 0x0030;
  1137. break;
  1138. /* pad character specified */
  1139. case FLAG_PAREN:
  1140. /* TODO test that all four are numbers */
  1141. /* first four characters are hex values for pad char */
  1142. info->fPadChar = (char16_t)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. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
  1146. /* final character is ignored */
  1147. alias++;
  1148. break;
  1149. }
  1150. }
  1151. /* Get the width */
  1152. /* width is specified out of line */
  1153. if(*alias == SPEC_ASTERISK) {
  1154. info->fWidth = -2;
  1155. /* Skip the '*' */
  1156. alias++;
  1157. /* Save the current position */
  1158. backup = alias;
  1159. /* handle positional parameters */
  1160. if(ISDIGIT(*alias)) {
  1161. spec.fWidthPos = *alias++ - DIGIT_ZERO;
  1162. while(ISDIGIT(*alias)) {
  1163. spec.fWidthPos *= 10;
  1164. spec.fWidthPos += *alias++ - DIGIT_ZERO;
  1165. }
  1166. }
  1167. /* if there is no '$', don't read anything */
  1168. if(*alias != SPEC_DOLLARSIGN) {
  1169. spec.fWidthPos = -1;
  1170. alias = backup;
  1171. }
  1172. /* munge the '$' */
  1173. else
  1174. alias++;
  1175. }
  1176. /* read the width, if present */
  1177. else if(ISDIGIT(*alias)){
  1178. info->fWidth = *alias++ - DIGIT_ZERO;
  1179. while(ISDIGIT(*alias)) {
  1180. info->fWidth *= 10;
  1181. info->fWidth += *alias++ - DIGIT_ZERO;
  1182. }
  1183. }
  1184. /* Get the precision */
  1185. if(*alias == SPEC_PERIOD) {
  1186. /* eat up the '.' */
  1187. alias++;
  1188. /* precision is specified out of line */
  1189. if(*alias == SPEC_ASTERISK) {
  1190. info->fPrecision = -2;
  1191. /* Skip the '*' */
  1192. alias++;
  1193. /* save the current position */
  1194. backup = alias;
  1195. /* handle positional parameters */
  1196. if(ISDIGIT(*alias)) {
  1197. spec.fPrecisionPos = *alias++ - DIGIT_ZERO;
  1198. while(ISDIGIT(*alias)) {
  1199. spec.fPrecisionPos *= 10;
  1200. spec.fPrecisionPos += *alias++ - DIGIT_ZERO;
  1201. }
  1202. /* if there is no '$', don't read anything */
  1203. if(*alias != SPEC_DOLLARSIGN) {
  1204. spec.fPrecisionPos = -1;
  1205. alias = backup;
  1206. }
  1207. else {
  1208. /* munge the '$' */
  1209. alias++;
  1210. }
  1211. }
  1212. }
  1213. /* read the precision */
  1214. else if(ISDIGIT(*alias)){
  1215. info->fPrecision = *alias++ - DIGIT_ZERO;
  1216. while(ISDIGIT(*alias)) {
  1217. info->fPrecision *= 10;
  1218. info->fPrecision += *alias++ - DIGIT_ZERO;
  1219. }
  1220. }
  1221. }
  1222. /* Get any modifiers */
  1223. if(ISMOD(*alias)) {
  1224. switch(*alias++) {
  1225. /* short */
  1226. case MOD_H:
  1227. info->fIsShort = true;
  1228. break;
  1229. /* long or long long */
  1230. case MOD_LOWERL:
  1231. if(*alias == MOD_LOWERL) {
  1232. info->fIsLongLong = true;
  1233. /* skip over the next 'l' */
  1234. alias++;
  1235. }
  1236. else
  1237. info->fIsLong = true;
  1238. break;
  1239. /* long double */
  1240. case MOD_L:
  1241. info->fIsLongDouble = true;
  1242. break;
  1243. }
  1244. }
  1245. /* finally, get the specifier letter */
  1246. info->fSpec = *alias++;
  1247. info->fOrigSpec = info->fSpec;
  1248. /* fill in the precision and width, if specified out of line */
  1249. /* width specified out of line */
  1250. if(spec.fInfo.fWidth == -2) {
  1251. if(spec.fWidthPos == -1) {
  1252. /* read the width from the argument list */
  1253. info->fWidth = va_arg(ap, int32_t);
  1254. }
  1255. /* else handle positional parameter */
  1256. /* if it's negative, take the absolute value and set left alignment */
  1257. if(info->fWidth < 0) {
  1258. info->fWidth *= -1; /* Make positive */
  1259. info->fLeft = true;
  1260. }
  1261. }
  1262. /* precision specified out of line */
  1263. if(info->fPrecision == -2) {
  1264. if(spec.fPrecisionPos == -1) {
  1265. /* read the precision from the argument list */
  1266. info->fPrecision = va_arg(ap, int32_t);
  1267. }
  1268. /* else handle positional parameter */
  1269. /* if it's negative, set it to zero */
  1270. if(info->fPrecision < 0)
  1271. info->fPrecision = 0;
  1272. }
  1273. handlerNum = (uint16_t)(info->fSpec - UPRINTF_BASE_FMT_HANDLERS);
  1274. if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) {
  1275. /* query the info function for argument information */
  1276. argType = g_u_printf_infos[ handlerNum ].info;
  1277. /* goto the correct argument on arg_list if position is specified */
  1278. if (spec.fArgPos > 0) {
  1279. /* offset position by 1 */
  1280. spec.fArgPos--;
  1281. switch(argType) {
  1282. case ufmt_count:
  1283. /* set the spec's width to the # of chars written */
  1284. info->fWidth = *written;
  1285. /* fall through to set the pointer */
  1286. U_FALLTHROUGH;
  1287. case ufmt_string:
  1288. case ufmt_ustring:
  1289. case ufmt_pointer:
  1290. args.ptrValue = arglist[spec.fArgPos].ptrValue;
  1291. break;
  1292. case ufmt_char:
  1293. case ufmt_uchar:
  1294. case ufmt_int:
  1295. args.int64Value = arglist[spec.fArgPos].int64Value;
  1296. break;
  1297. case ufmt_float:
  1298. args.floatValue = arglist[spec.fArgPos].floatValue;
  1299. break;
  1300. case ufmt_double:
  1301. args.doubleValue = arglist[spec.fArgPos].doubleValue;
  1302. break;
  1303. default:
  1304. /* else args is ignored */
  1305. args.ptrValue = nullptr;
  1306. break;
  1307. }
  1308. } else { /* no positional argument specified */
  1309. switch(argType) {
  1310. case ufmt_count:
  1311. /* set the spec's width to the # of chars written */
  1312. info->fWidth = *written;
  1313. /* fall through to set the pointer */
  1314. U_FALLTHROUGH;
  1315. case ufmt_string:
  1316. case ufmt_ustring:
  1317. case ufmt_pointer:
  1318. args.ptrValue = va_arg(ap, void*);
  1319. break;
  1320. case ufmt_char:
  1321. case ufmt_uchar:
  1322. case ufmt_int:
  1323. if (info->fIsLongLong) {
  1324. args.int64Value = va_arg(ap, int64_t);
  1325. }
  1326. else {
  1327. args.int64Value = va_arg(ap, int32_t);
  1328. }
  1329. break;
  1330. case ufmt_float:
  1331. args.floatValue = (float) va_arg(ap, double);
  1332. break;
  1333. case ufmt_double:
  1334. args.doubleValue = va_arg(ap, double);
  1335. break;
  1336. default:
  1337. /* else args is ignored */
  1338. args.ptrValue = nullptr;
  1339. break;
  1340. }
  1341. }
  1342. /* call the handler function */
  1343. handler = g_u_printf_infos[ handlerNum ].handler;
  1344. if (handler != nullptr) {
  1345. *written += (*handler)(streamHandler, context, formatBundle, info, &args);
  1346. }
  1347. else {
  1348. /* just echo unknown tags */
  1349. *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias));
  1350. }
  1351. }
  1352. else {
  1353. /* just echo unknown tags */
  1354. *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias));
  1355. }
  1356. }
  1357. /* delete parsed argument list */
  1358. if (arglist != nullptr) {
  1359. uprv_free(arglist);
  1360. }
  1361. /* return # of characters in this format that have been parsed. */
  1362. return (int32_t)(alias - fmt);
  1363. }
  1364. #endif /* #if !UCONFIG_NO_FORMATTING */