uscanf_p.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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 uscnnf_p.c
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 12/02/98 stephen Creation.
  17. * 03/13/99 stephen Modified for new C API.
  18. *******************************************************************************
  19. */
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
  22. #include "unicode/uchar.h"
  23. #include "unicode/ustring.h"
  24. #include "unicode/unum.h"
  25. #include "unicode/udat.h"
  26. #include "unicode/uset.h"
  27. #include "uscanf.h"
  28. #include "ufmt_cmn.h"
  29. #include "ufile.h"
  30. #include "locbund.h"
  31. #include "cmemory.h"
  32. #include "ustr_cnv.h"
  33. /* flag characters for u_scanf */
  34. #define FLAG_ASTERISK 0x002A
  35. #define FLAG_PAREN 0x0028
  36. #define ISFLAG(s) (s) == FLAG_ASTERISK || \
  37. (s) == FLAG_PAREN
  38. /* special characters for u_scanf */
  39. #define SPEC_DOLLARSIGN 0x0024
  40. /* unicode digits */
  41. #define DIGIT_ZERO 0x0030
  42. #define DIGIT_ONE 0x0031
  43. #define DIGIT_TWO 0x0032
  44. #define DIGIT_THREE 0x0033
  45. #define DIGIT_FOUR 0x0034
  46. #define DIGIT_FIVE 0x0035
  47. #define DIGIT_SIX 0x0036
  48. #define DIGIT_SEVEN 0x0037
  49. #define DIGIT_EIGHT 0x0038
  50. #define DIGIT_NINE 0x0039
  51. #define ISDIGIT(s) (s) == DIGIT_ZERO || \
  52. (s) == DIGIT_ONE || \
  53. (s) == DIGIT_TWO || \
  54. (s) == DIGIT_THREE || \
  55. (s) == DIGIT_FOUR || \
  56. (s) == DIGIT_FIVE || \
  57. (s) == DIGIT_SIX || \
  58. (s) == DIGIT_SEVEN || \
  59. (s) == DIGIT_EIGHT || \
  60. (s) == DIGIT_NINE
  61. /* u_scanf modifiers */
  62. #define MOD_H 0x0068
  63. #define MOD_LOWERL 0x006C
  64. #define MOD_L 0x004C
  65. #define ISMOD(s) (s) == MOD_H || \
  66. (s) == MOD_LOWERL || \
  67. (s) == MOD_L
  68. /**
  69. * Struct encapsulating a single uscanf format specification.
  70. */
  71. typedef struct u_scanf_spec_info {
  72. int32_t fWidth; /* Width */
  73. char16_t fSpec; /* Format specification */
  74. char16_t fPadChar; /* Padding character */
  75. UBool fSkipArg; /* true if arg should be skipped */
  76. UBool fIsLongDouble; /* L flag */
  77. UBool fIsShort; /* h flag */
  78. UBool fIsLong; /* l flag */
  79. UBool fIsLongLong; /* ll flag */
  80. UBool fIsString; /* true if this is a NUL-terminated string. */
  81. } u_scanf_spec_info;
  82. /**
  83. * Struct encapsulating a single u_scanf format specification.
  84. */
  85. typedef struct u_scanf_spec {
  86. u_scanf_spec_info fInfo; /* Information on this spec */
  87. int32_t fArgPos; /* Position of data in arg list */
  88. } u_scanf_spec;
  89. /**
  90. * Parse a single u_scanf format specifier in Unicode.
  91. * @param fmt A pointer to a '%' character in a u_scanf format specification.
  92. * @param spec A pointer to a <TT>u_scanf_spec</TT> to receive the parsed
  93. * format specifier.
  94. * @return The number of characters contained in this specifier.
  95. */
  96. static int32_t
  97. u_scanf_parse_spec (const char16_t *fmt,
  98. u_scanf_spec *spec)
  99. {
  100. const char16_t *s = fmt;
  101. const char16_t *backup;
  102. u_scanf_spec_info *info = &(spec->fInfo);
  103. /* initialize spec to default values */
  104. spec->fArgPos = -1;
  105. info->fWidth = -1;
  106. info->fSpec = 0x0000;
  107. info->fPadChar = 0x0020;
  108. info->fSkipArg = false;
  109. info->fIsLongDouble = false;
  110. info->fIsShort = false;
  111. info->fIsLong = false;
  112. info->fIsLongLong = false;
  113. info->fIsString = true;
  114. /* skip over the initial '%' */
  115. s++;
  116. /* Check for positional argument */
  117. if(ISDIGIT(*s)) {
  118. /* Save the current position */
  119. backup = s;
  120. /* handle positional parameters */
  121. if(ISDIGIT(*s)) {
  122. spec->fArgPos = *s++ - DIGIT_ZERO;
  123. while(ISDIGIT(*s)) {
  124. spec->fArgPos *= 10;
  125. spec->fArgPos += *s++ - DIGIT_ZERO;
  126. }
  127. }
  128. /* if there is no '$', don't read anything */
  129. if(*s != SPEC_DOLLARSIGN) {
  130. spec->fArgPos = -1;
  131. s = backup;
  132. }
  133. /* munge the '$' */
  134. else
  135. s++;
  136. }
  137. /* Get any format flags */
  138. while(ISFLAG(*s)) {
  139. switch(*s++) {
  140. /* skip argument */
  141. case FLAG_ASTERISK:
  142. info->fSkipArg = true;
  143. break;
  144. /* pad character specified */
  145. case FLAG_PAREN:
  146. /* first four characters are hex values for pad char */
  147. info->fPadChar = static_cast<char16_t>(ufmt_digitvalue(*s++));
  148. info->fPadChar = static_cast<char16_t>((info->fPadChar * 16) + ufmt_digitvalue(*s++));
  149. info->fPadChar = static_cast<char16_t>((info->fPadChar * 16) + ufmt_digitvalue(*s++));
  150. info->fPadChar = static_cast<char16_t>((info->fPadChar * 16) + ufmt_digitvalue(*s++));
  151. /* final character is ignored */
  152. s++;
  153. break;
  154. }
  155. }
  156. /* Get the width */
  157. if(ISDIGIT(*s)){
  158. info->fWidth = *s++ - DIGIT_ZERO;
  159. while(ISDIGIT(*s)) {
  160. info->fWidth *= 10;
  161. info->fWidth += *s++ - DIGIT_ZERO;
  162. }
  163. }
  164. /* Get any modifiers */
  165. if(ISMOD(*s)) {
  166. switch(*s++) {
  167. /* short */
  168. case MOD_H:
  169. info->fIsShort = true;
  170. break;
  171. /* long or long long */
  172. case MOD_LOWERL:
  173. if(*s == MOD_LOWERL) {
  174. info->fIsLongLong = true;
  175. /* skip over the next 'l' */
  176. s++;
  177. }
  178. else
  179. info->fIsLong = true;
  180. break;
  181. /* long double */
  182. case MOD_L:
  183. info->fIsLongDouble = true;
  184. break;
  185. }
  186. }
  187. /* finally, get the specifier letter */
  188. info->fSpec = *s++;
  189. /* return # of characters in this specifier */
  190. return static_cast<int32_t>(s - fmt);
  191. }
  192. #define UP_PERCENT 0x0025
  193. /* ANSI style formatting */
  194. /* Use US-ASCII characters only for formatting */
  195. /* % */
  196. #define UFMT_SIMPLE_PERCENT {ufmt_simple_percent, u_scanf_simple_percent_handler}
  197. /* s */
  198. #define UFMT_STRING {ufmt_string, u_scanf_string_handler}
  199. /* c */
  200. #define UFMT_CHAR {ufmt_string, u_scanf_char_handler}
  201. /* d, i */
  202. #define UFMT_INT {ufmt_int, u_scanf_integer_handler}
  203. /* u */
  204. #define UFMT_UINT {ufmt_int, u_scanf_uinteger_handler}
  205. /* o */
  206. #define UFMT_OCTAL {ufmt_int, u_scanf_octal_handler}
  207. /* x, X */
  208. #define UFMT_HEX {ufmt_int, u_scanf_hex_handler}
  209. /* f */
  210. #define UFMT_DOUBLE {ufmt_double, u_scanf_double_handler}
  211. /* e, E */
  212. #define UFMT_SCIENTIFIC {ufmt_double, u_scanf_scientific_handler}
  213. /* g, G */
  214. #define UFMT_SCIDBL {ufmt_double, u_scanf_scidbl_handler}
  215. /* n */
  216. #define UFMT_COUNT {ufmt_count, u_scanf_count_handler}
  217. /* [ */
  218. #define UFMT_SCANSET {ufmt_string, u_scanf_scanset_handler}
  219. /* non-ANSI extensions */
  220. /* Use US-ASCII characters only for formatting */
  221. /* p */
  222. #define UFMT_POINTER {ufmt_pointer, u_scanf_pointer_handler}
  223. /* V */
  224. #define UFMT_SPELLOUT {ufmt_double, u_scanf_spellout_handler}
  225. /* P */
  226. #define UFMT_PERCENT {ufmt_double, u_scanf_percent_handler}
  227. /* C K is old format */
  228. #define UFMT_UCHAR {ufmt_uchar, u_scanf_uchar_handler}
  229. /* S U is old format */
  230. #define UFMT_USTRING {ufmt_ustring, u_scanf_ustring_handler}
  231. #define UFMT_EMPTY {ufmt_empty, nullptr}
  232. /**
  233. * A u_scanf handler function.
  234. * A u_scanf handler is responsible for handling a single u_scanf
  235. * format specification, for example 'd' or 's'.
  236. * @param stream The UFILE to which to write output.
  237. * @param info A pointer to a <TT>u_scanf_spec_info</TT> struct containing
  238. * information on the format specification.
  239. * @param args A pointer to the argument data
  240. * @param fmt A pointer to the first character in the format string
  241. * following the spec.
  242. * @param fmtConsumed On output, set to the number of characters consumed
  243. * in <TT>fmt</TT>. Do nothing, if the argument isn't variable width.
  244. * @param argConverted The number of arguments converted and assigned, or -1 if an
  245. * error occurred.
  246. * @return The number of code points consumed during reading.
  247. */
  248. typedef int32_t (*u_scanf_handler) (UFILE *stream,
  249. u_scanf_spec_info *info,
  250. ufmt_args *args,
  251. const char16_t *fmt,
  252. int32_t *fmtConsumed,
  253. int32_t *argConverted);
  254. typedef struct u_scanf_info {
  255. ufmt_type_info info;
  256. u_scanf_handler handler;
  257. } u_scanf_info;
  258. #define USCANF_NUM_FMT_HANDLERS 108
  259. #define USCANF_SYMBOL_BUFFER_SIZE 8
  260. /* We do not use handlers for 0-0x1f */
  261. #define USCANF_BASE_FMT_HANDLERS 0x20
  262. static int32_t
  263. u_scanf_skip_leading_ws(UFILE *input,
  264. char16_t pad)
  265. {
  266. char16_t c;
  267. int32_t count = 0;
  268. UBool isNotEOF;
  269. /* skip all leading ws in the input */
  270. while (((isNotEOF = ufile_getch(input, &c)) == static_cast<UBool>(true)) && (c == pad || u_isWhitespace(c)))
  271. {
  272. count++;
  273. }
  274. /* put the final character back on the input */
  275. if(isNotEOF)
  276. u_fungetc(c, input);
  277. return count;
  278. }
  279. /* TODO: Is always skipping the prefix symbol as a positive sign a good idea in all locales? */
  280. static int32_t
  281. u_scanf_skip_leading_positive_sign(UFILE *input,
  282. UNumberFormat *format,
  283. UErrorCode *status)
  284. {
  285. char16_t c;
  286. int32_t count = 0;
  287. UBool isNotEOF;
  288. char16_t plusSymbol[USCANF_SYMBOL_BUFFER_SIZE];
  289. int32_t symbolLen;
  290. UErrorCode localStatus = U_ZERO_ERROR;
  291. if (U_SUCCESS(*status)) {
  292. symbolLen = unum_getSymbol(format,
  293. UNUM_PLUS_SIGN_SYMBOL,
  294. plusSymbol,
  295. UPRV_LENGTHOF(plusSymbol),
  296. &localStatus);
  297. if (U_SUCCESS(localStatus)) {
  298. /* skip all leading ws in the input */
  299. while (((isNotEOF = ufile_getch(input, &c)) == static_cast<UBool>(true)) && (count < symbolLen && c == plusSymbol[count]))
  300. {
  301. count++;
  302. }
  303. /* put the final character back on the input */
  304. if(isNotEOF) {
  305. u_fungetc(c, input);
  306. }
  307. }
  308. }
  309. return count;
  310. }
  311. static int32_t
  312. u_scanf_simple_percent_handler(UFILE *input,
  313. u_scanf_spec_info *info,
  314. ufmt_args *args,
  315. const char16_t *fmt,
  316. int32_t *fmtConsumed,
  317. int32_t *argConverted)
  318. {
  319. (void)info;
  320. (void)args;
  321. (void)fmt;
  322. (void)fmtConsumed;
  323. /* make sure the next character in the input is a percent */
  324. *argConverted = 0;
  325. if(u_fgetc(input) != 0x0025) {
  326. *argConverted = -1;
  327. }
  328. return 1;
  329. }
  330. static int32_t
  331. u_scanf_count_handler(UFILE *input,
  332. u_scanf_spec_info *info,
  333. ufmt_args *args,
  334. const char16_t *fmt,
  335. int32_t *fmtConsumed,
  336. int32_t *argConverted)
  337. {
  338. (void)input;
  339. (void)fmt;
  340. (void)fmtConsumed;
  341. /* in the special case of count, the u_scanf_spec_info's width */
  342. /* will contain the # of items converted thus far */
  343. if (!info->fSkipArg) {
  344. if (info->fIsShort)
  345. *static_cast<int16_t*>(args[0].ptrValue) = static_cast<int16_t>(UINT16_MAX & info->fWidth);
  346. else if (info->fIsLongLong)
  347. *static_cast<int64_t*>(args[0].ptrValue) = info->fWidth;
  348. else
  349. *static_cast<int32_t*>(args[0].ptrValue) = static_cast<int32_t>(UINT32_MAX & info->fWidth);
  350. }
  351. *argConverted = 0;
  352. /* we converted 0 args */
  353. return 0;
  354. }
  355. static int32_t
  356. u_scanf_double_handler(UFILE *input,
  357. u_scanf_spec_info *info,
  358. ufmt_args *args,
  359. const char16_t *fmt,
  360. int32_t *fmtConsumed,
  361. int32_t *argConverted)
  362. {
  363. (void)fmt;
  364. (void)fmtConsumed;
  365. int32_t len;
  366. double num;
  367. UNumberFormat *format;
  368. int32_t parsePos = 0;
  369. int32_t skipped;
  370. UErrorCode status = U_ZERO_ERROR;
  371. /* skip all ws in the input */
  372. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  373. /* fill the input's internal buffer */
  374. ufile_fill_uchar_buffer(input);
  375. /* determine the size of the input's buffer */
  376. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  377. /* truncate to the width, if specified */
  378. if(info->fWidth != -1)
  379. len = ufmt_min(len, info->fWidth);
  380. /* get the formatter */
  381. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL);
  382. /* handle error */
  383. if (format == nullptr)
  384. return 0;
  385. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  386. skipped += u_scanf_skip_leading_positive_sign(input, format, &status);
  387. /* parse the number */
  388. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  389. if (!info->fSkipArg) {
  390. if (info->fIsLong)
  391. *static_cast<double*>(args[0].ptrValue) = num;
  392. else if (info->fIsLongDouble)
  393. *static_cast<long double*>(args[0].ptrValue) = num;
  394. else
  395. *static_cast<float*>(args[0].ptrValue) = static_cast<float>(num);
  396. }
  397. /* mask off any necessary bits */
  398. /* if(! info->fIsLong_double)
  399. num &= DBL_MAX;*/
  400. /* update the input's position to reflect consumed data */
  401. input->str.fPos += parsePos;
  402. /* we converted 1 arg */
  403. *argConverted = !info->fSkipArg;
  404. return parsePos + skipped;
  405. }
  406. #define UPRINTF_SYMBOL_BUFFER_SIZE 8
  407. static int32_t
  408. u_scanf_scientific_handler(UFILE *input,
  409. u_scanf_spec_info *info,
  410. ufmt_args *args,
  411. const char16_t *fmt,
  412. int32_t *fmtConsumed,
  413. int32_t *argConverted)
  414. {
  415. (void)fmt;
  416. (void)fmtConsumed;
  417. int32_t len;
  418. double num;
  419. UNumberFormat *format;
  420. int32_t parsePos = 0;
  421. int32_t skipped;
  422. UErrorCode status = U_ZERO_ERROR;
  423. char16_t srcExpBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
  424. int32_t srcLen, expLen;
  425. char16_t expBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
  426. /* skip all ws in the input */
  427. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  428. /* fill the input's internal buffer */
  429. ufile_fill_uchar_buffer(input);
  430. /* determine the size of the input's buffer */
  431. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  432. /* truncate to the width, if specified */
  433. if(info->fWidth != -1)
  434. len = ufmt_min(len, info->fWidth);
  435. /* get the formatter */
  436. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SCIENTIFIC);
  437. /* handle error */
  438. if (format == nullptr)
  439. return 0;
  440. /* set the appropriate flags on the formatter */
  441. srcLen = unum_getSymbol(format,
  442. UNUM_EXPONENTIAL_SYMBOL,
  443. srcExpBuf,
  444. sizeof(srcExpBuf),
  445. &status);
  446. /* Upper/lower case the e */
  447. if (info->fSpec == static_cast<char16_t>(0x65) /* e */) {
  448. expLen = u_strToLower(expBuf, static_cast<int32_t>(sizeof(expBuf)),
  449. srcExpBuf, srcLen,
  450. input->str.fBundle.fLocale,
  451. &status);
  452. }
  453. else {
  454. expLen = u_strToUpper(expBuf, static_cast<int32_t>(sizeof(expBuf)),
  455. srcExpBuf, srcLen,
  456. input->str.fBundle.fLocale,
  457. &status);
  458. }
  459. unum_setSymbol(format,
  460. UNUM_EXPONENTIAL_SYMBOL,
  461. expBuf,
  462. expLen,
  463. &status);
  464. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  465. skipped += u_scanf_skip_leading_positive_sign(input, format, &status);
  466. /* parse the number */
  467. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  468. if (!info->fSkipArg) {
  469. if (info->fIsLong)
  470. *static_cast<double*>(args[0].ptrValue) = num;
  471. else if (info->fIsLongDouble)
  472. *static_cast<long double*>(args[0].ptrValue) = num;
  473. else
  474. *static_cast<float*>(args[0].ptrValue) = static_cast<float>(num);
  475. }
  476. /* mask off any necessary bits */
  477. /* if(! info->fIsLong_double)
  478. num &= DBL_MAX;*/
  479. /* update the input's position to reflect consumed data */
  480. input->str.fPos += parsePos;
  481. /* we converted 1 arg */
  482. *argConverted = !info->fSkipArg;
  483. return parsePos + skipped;
  484. }
  485. static int32_t
  486. u_scanf_scidbl_handler(UFILE *input,
  487. u_scanf_spec_info *info,
  488. ufmt_args *args,
  489. const char16_t *fmt,
  490. int32_t *fmtConsumed,
  491. int32_t *argConverted)
  492. {
  493. (void)fmt;
  494. (void)fmtConsumed;
  495. int32_t len;
  496. double num;
  497. UNumberFormat *scientificFormat, *genericFormat;
  498. /*int32_t scientificResult, genericResult;*/
  499. double scientificResult, genericResult;
  500. int32_t scientificParsePos = 0, genericParsePos = 0, parsePos = 0;
  501. int32_t skipped;
  502. UErrorCode scientificStatus = U_ZERO_ERROR;
  503. UErrorCode genericStatus = U_ZERO_ERROR;
  504. /* since we can't determine by scanning the characters whether */
  505. /* a number was formatted in the 'f' or 'g' styles, parse the */
  506. /* string with both formatters, and assume whichever one */
  507. /* parsed the most is the correct formatter to use */
  508. /* skip all ws in the input */
  509. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  510. /* fill the input's internal buffer */
  511. ufile_fill_uchar_buffer(input);
  512. /* determine the size of the input's buffer */
  513. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  514. /* truncate to the width, if specified */
  515. if(info->fWidth != -1)
  516. len = ufmt_min(len, info->fWidth);
  517. /* get the formatters */
  518. scientificFormat = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SCIENTIFIC);
  519. genericFormat = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL);
  520. /* handle error */
  521. if (scientificFormat == nullptr || genericFormat == nullptr)
  522. return 0;
  523. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  524. skipped += u_scanf_skip_leading_positive_sign(input, genericFormat, &genericStatus);
  525. /* parse the number using each format*/
  526. scientificResult = unum_parseDouble(scientificFormat, input->str.fPos, len,
  527. &scientificParsePos, &scientificStatus);
  528. genericResult = unum_parseDouble(genericFormat, input->str.fPos, len,
  529. &genericParsePos, &genericStatus);
  530. /* determine which parse made it farther */
  531. if(scientificParsePos > genericParsePos) {
  532. /* stash the result in num */
  533. num = scientificResult;
  534. /* update the input's position to reflect consumed data */
  535. parsePos += scientificParsePos;
  536. }
  537. else {
  538. /* stash the result in num */
  539. num = genericResult;
  540. /* update the input's position to reflect consumed data */
  541. parsePos += genericParsePos;
  542. }
  543. input->str.fPos += parsePos;
  544. if (!info->fSkipArg) {
  545. if (info->fIsLong)
  546. *static_cast<double*>(args[0].ptrValue) = num;
  547. else if (info->fIsLongDouble)
  548. *static_cast<long double*>(args[0].ptrValue) = num;
  549. else
  550. *static_cast<float*>(args[0].ptrValue) = static_cast<float>(num);
  551. }
  552. /* mask off any necessary bits */
  553. /* if(! info->fIsLong_double)
  554. num &= DBL_MAX;*/
  555. /* we converted 1 arg */
  556. *argConverted = !info->fSkipArg;
  557. return parsePos + skipped;
  558. }
  559. static int32_t
  560. u_scanf_integer_handler(UFILE *input,
  561. u_scanf_spec_info *info,
  562. ufmt_args *args,
  563. const char16_t *fmt,
  564. int32_t *fmtConsumed,
  565. int32_t *argConverted)
  566. {
  567. (void)fmt;
  568. (void)fmtConsumed;
  569. int32_t len;
  570. void* num = args[0].ptrValue;
  571. UNumberFormat *format, *localFormat;
  572. int32_t parsePos = 0;
  573. int32_t skipped;
  574. int32_t parseIntOnly = 0;
  575. UErrorCode status = U_ZERO_ERROR;
  576. int64_t result;
  577. /* skip all ws in the input */
  578. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  579. /* fill the input's internal buffer */
  580. ufile_fill_uchar_buffer(input);
  581. /* determine the size of the input's buffer */
  582. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  583. /* truncate to the width, if specified */
  584. if(info->fWidth != -1)
  585. len = ufmt_min(len, info->fWidth);
  586. /* get the formatter */
  587. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL);
  588. /* handle error */
  589. if (format == nullptr)
  590. return 0;
  591. /* for integer types, do not attempt to parse fractions */
  592. localFormat = unum_clone(format, &status);
  593. if(U_FAILURE(status))
  594. return 0;
  595. if(info->fSpec == 'd' || info->fSpec == 'i' || info->fSpec == 'u')
  596. parseIntOnly = 1;
  597. unum_setAttribute(localFormat, UNUM_PARSE_INT_ONLY, parseIntOnly);
  598. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  599. skipped += u_scanf_skip_leading_positive_sign(input, localFormat, &status);
  600. /* parse the number */
  601. result = unum_parseInt64(localFormat, input->str.fPos, len, &parsePos, &status);
  602. /* mask off any necessary bits */
  603. if (!info->fSkipArg) {
  604. if (info->fIsShort)
  605. *static_cast<int16_t*>(num) = static_cast<int16_t>(UINT16_MAX & result);
  606. else if (info->fIsLongLong)
  607. *static_cast<int64_t*>(num) = result;
  608. else
  609. *static_cast<int32_t*>(num) = static_cast<int32_t>(UINT32_MAX & result);
  610. }
  611. /* update the input's position to reflect consumed data */
  612. input->str.fPos += parsePos;
  613. /* cleanup cloned formatter */
  614. unum_close(localFormat);
  615. /* we converted 1 arg */
  616. *argConverted = !info->fSkipArg;
  617. return parsePos + skipped;
  618. }
  619. static int32_t
  620. u_scanf_uinteger_handler(UFILE *input,
  621. u_scanf_spec_info *info,
  622. ufmt_args *args,
  623. const char16_t *fmt,
  624. int32_t *fmtConsumed,
  625. int32_t *argConverted)
  626. {
  627. /* TODO Fix this when Numberformat handles uint64_t */
  628. return u_scanf_integer_handler(input, info, args, fmt, fmtConsumed, argConverted);
  629. }
  630. static int32_t
  631. u_scanf_percent_handler(UFILE *input,
  632. u_scanf_spec_info *info,
  633. ufmt_args *args,
  634. const char16_t *fmt,
  635. int32_t *fmtConsumed,
  636. int32_t *argConverted)
  637. {
  638. (void)fmt;
  639. (void)fmtConsumed;
  640. int32_t len;
  641. double num;
  642. UNumberFormat *format;
  643. int32_t parsePos = 0;
  644. UErrorCode status = U_ZERO_ERROR;
  645. /* skip all ws in the input */
  646. u_scanf_skip_leading_ws(input, info->fPadChar);
  647. /* fill the input's internal buffer */
  648. ufile_fill_uchar_buffer(input);
  649. /* determine the size of the input's buffer */
  650. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  651. /* truncate to the width, if specified */
  652. if(info->fWidth != -1)
  653. len = ufmt_min(len, info->fWidth);
  654. /* get the formatter */
  655. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_PERCENT);
  656. /* handle error */
  657. if (format == nullptr)
  658. return 0;
  659. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  660. u_scanf_skip_leading_positive_sign(input, format, &status);
  661. /* parse the number */
  662. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  663. if (!info->fSkipArg) {
  664. *static_cast<double*>(args[0].ptrValue) = num;
  665. }
  666. /* mask off any necessary bits */
  667. /* if(! info->fIsLong_double)
  668. num &= DBL_MAX;*/
  669. /* update the input's position to reflect consumed data */
  670. input->str.fPos += parsePos;
  671. /* we converted 1 arg */
  672. *argConverted = !info->fSkipArg;
  673. return parsePos;
  674. }
  675. static int32_t
  676. u_scanf_string_handler(UFILE *input,
  677. u_scanf_spec_info *info,
  678. ufmt_args *args,
  679. const char16_t *fmt,
  680. int32_t *fmtConsumed,
  681. int32_t *argConverted)
  682. {
  683. (void)fmt;
  684. (void)fmtConsumed;
  685. const char16_t *source;
  686. UConverter *conv;
  687. char* arg = static_cast<char*>(args[0].ptrValue);
  688. char *alias = arg;
  689. char *limit;
  690. UErrorCode status = U_ZERO_ERROR;
  691. int32_t count;
  692. int32_t skipped = 0;
  693. char16_t c;
  694. UBool isNotEOF = false;
  695. /* skip all ws in the input */
  696. if (info->fIsString) {
  697. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  698. }
  699. /* get the string one character at a time, truncating to the width */
  700. count = 0;
  701. /* open the default converter */
  702. conv = u_getDefaultConverter(&status);
  703. if(U_FAILURE(status))
  704. return -1;
  705. while( (info->fWidth == -1 || count < info->fWidth)
  706. && ((isNotEOF = ufile_getch(input, &c)) == static_cast<UBool>(true))
  707. && (!info->fIsString || (c != info->fPadChar && !u_isWhitespace(c))))
  708. {
  709. if (!info->fSkipArg) {
  710. /* put the character from the input onto the target */
  711. source = &c;
  712. /* Since we do this one character at a time, do it this way. */
  713. if (info->fWidth > 0) {
  714. limit = alias + info->fWidth - count;
  715. }
  716. else {
  717. limit = alias + ucnv_getMaxCharSize(conv);
  718. }
  719. /* convert the character to the default codepage */
  720. ucnv_fromUnicode(conv, &alias, limit, &source, source + 1,
  721. nullptr, true, &status);
  722. if(U_FAILURE(status)) {
  723. /* clean up */
  724. u_releaseDefaultConverter(conv);
  725. return -1;
  726. }
  727. }
  728. /* increment the count */
  729. ++count;
  730. }
  731. /* put the final character we read back on the input */
  732. if (!info->fSkipArg) {
  733. if ((info->fWidth == -1 || count < info->fWidth) && isNotEOF)
  734. u_fungetc(c, input);
  735. /* add the terminator */
  736. if (info->fIsString) {
  737. *alias = 0x00;
  738. }
  739. }
  740. /* clean up */
  741. u_releaseDefaultConverter(conv);
  742. /* we converted 1 arg */
  743. *argConverted = !info->fSkipArg;
  744. return count + skipped;
  745. }
  746. static int32_t
  747. u_scanf_char_handler(UFILE *input,
  748. u_scanf_spec_info *info,
  749. ufmt_args *args,
  750. const char16_t *fmt,
  751. int32_t *fmtConsumed,
  752. int32_t *argConverted)
  753. {
  754. if (info->fWidth < 0) {
  755. info->fWidth = 1;
  756. }
  757. info->fIsString = false;
  758. return u_scanf_string_handler(input, info, args, fmt, fmtConsumed, argConverted);
  759. }
  760. static int32_t
  761. u_scanf_ustring_handler(UFILE *input,
  762. u_scanf_spec_info *info,
  763. ufmt_args *args,
  764. const char16_t *fmt,
  765. int32_t *fmtConsumed,
  766. int32_t *argConverted)
  767. {
  768. (void)fmt;
  769. (void)fmtConsumed;
  770. char16_t* arg = static_cast<char16_t*>(args[0].ptrValue);
  771. char16_t *alias = arg;
  772. int32_t count;
  773. int32_t skipped = 0;
  774. char16_t c;
  775. UBool isNotEOF = false;
  776. /* skip all ws in the input */
  777. if (info->fIsString) {
  778. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  779. }
  780. /* get the string one character at a time, truncating to the width */
  781. count = 0;
  782. while( (info->fWidth == -1 || count < info->fWidth)
  783. && ((isNotEOF = ufile_getch(input, &c)) == static_cast<UBool>(true))
  784. && (!info->fIsString || (c != info->fPadChar && !u_isWhitespace(c))))
  785. {
  786. /* put the character from the input onto the target */
  787. if (!info->fSkipArg) {
  788. *alias++ = c;
  789. }
  790. /* increment the count */
  791. ++count;
  792. }
  793. /* put the final character we read back on the input */
  794. if (!info->fSkipArg) {
  795. if((info->fWidth == -1 || count < info->fWidth) && isNotEOF) {
  796. u_fungetc(c, input);
  797. }
  798. /* add the terminator */
  799. if (info->fIsString) {
  800. *alias = 0x0000;
  801. }
  802. }
  803. /* we converted 1 arg */
  804. *argConverted = !info->fSkipArg;
  805. return count + skipped;
  806. }
  807. static int32_t
  808. u_scanf_uchar_handler(UFILE *input,
  809. u_scanf_spec_info *info,
  810. ufmt_args *args,
  811. const char16_t *fmt,
  812. int32_t *fmtConsumed,
  813. int32_t *argConverted)
  814. {
  815. if (info->fWidth < 0) {
  816. info->fWidth = 1;
  817. }
  818. info->fIsString = false;
  819. return u_scanf_ustring_handler(input, info, args, fmt, fmtConsumed, argConverted);
  820. }
  821. static int32_t
  822. u_scanf_spellout_handler(UFILE *input,
  823. u_scanf_spec_info *info,
  824. ufmt_args *args,
  825. const char16_t *fmt,
  826. int32_t *fmtConsumed,
  827. int32_t *argConverted)
  828. {
  829. (void)fmt;
  830. (void)fmtConsumed;
  831. int32_t len;
  832. double num;
  833. UNumberFormat *format;
  834. int32_t parsePos = 0;
  835. int32_t skipped;
  836. UErrorCode status = U_ZERO_ERROR;
  837. /* skip all ws in the input */
  838. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  839. /* fill the input's internal buffer */
  840. ufile_fill_uchar_buffer(input);
  841. /* determine the size of the input's buffer */
  842. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  843. /* truncate to the width, if specified */
  844. if(info->fWidth != -1)
  845. len = ufmt_min(len, info->fWidth);
  846. /* get the formatter */
  847. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SPELLOUT);
  848. /* handle error */
  849. if (format == nullptr)
  850. return 0;
  851. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  852. /* This is not applicable to RBNF. */
  853. /*skipped += u_scanf_skip_leading_positive_sign(input, format, &status);*/
  854. /* parse the number */
  855. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  856. if (!info->fSkipArg) {
  857. *static_cast<double*>(args[0].ptrValue) = num;
  858. }
  859. /* mask off any necessary bits */
  860. /* if(! info->fIsLong_double)
  861. num &= DBL_MAX;*/
  862. /* update the input's position to reflect consumed data */
  863. input->str.fPos += parsePos;
  864. /* we converted 1 arg */
  865. *argConverted = !info->fSkipArg;
  866. return parsePos + skipped;
  867. }
  868. static int32_t
  869. u_scanf_hex_handler(UFILE *input,
  870. u_scanf_spec_info *info,
  871. ufmt_args *args,
  872. const char16_t *fmt,
  873. int32_t *fmtConsumed,
  874. int32_t *argConverted)
  875. {
  876. (void)fmt;
  877. (void)fmtConsumed;
  878. int32_t len;
  879. int32_t skipped;
  880. void* num = args[0].ptrValue;
  881. int64_t result;
  882. /* skip all ws in the input */
  883. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  884. /* fill the input's internal buffer */
  885. ufile_fill_uchar_buffer(input);
  886. /* determine the size of the input's buffer */
  887. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  888. /* truncate to the width, if specified */
  889. if(info->fWidth != -1)
  890. len = ufmt_min(len, info->fWidth);
  891. /* check for alternate form */
  892. if( *(input->str.fPos) == 0x0030 &&
  893. (*(input->str.fPos + 1) == 0x0078 || *(input->str.fPos + 1) == 0x0058) ) {
  894. /* skip the '0' and 'x' or 'X' if present */
  895. input->str.fPos += 2;
  896. len -= 2;
  897. }
  898. /* parse the number */
  899. result = ufmt_uto64(input->str.fPos, &len, 16);
  900. /* update the input's position to reflect consumed data */
  901. input->str.fPos += len;
  902. /* mask off any necessary bits */
  903. if (!info->fSkipArg) {
  904. if (info->fIsShort)
  905. *static_cast<int16_t*>(num) = static_cast<int16_t>(UINT16_MAX & result);
  906. else if (info->fIsLongLong)
  907. *static_cast<int64_t*>(num) = result;
  908. else
  909. *static_cast<int32_t*>(num) = static_cast<int32_t>(UINT32_MAX & result);
  910. }
  911. /* we converted 1 arg */
  912. *argConverted = !info->fSkipArg;
  913. return len + skipped;
  914. }
  915. static int32_t
  916. u_scanf_octal_handler(UFILE *input,
  917. u_scanf_spec_info *info,
  918. ufmt_args *args,
  919. const char16_t *fmt,
  920. int32_t *fmtConsumed,
  921. int32_t *argConverted)
  922. {
  923. (void)fmt;
  924. (void)fmtConsumed;
  925. int32_t len;
  926. int32_t skipped;
  927. void* num = args[0].ptrValue;
  928. int64_t result;
  929. /* skip all ws in the input */
  930. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  931. /* fill the input's internal buffer */
  932. ufile_fill_uchar_buffer(input);
  933. /* determine the size of the input's buffer */
  934. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  935. /* truncate to the width, if specified */
  936. if(info->fWidth != -1)
  937. len = ufmt_min(len, info->fWidth);
  938. /* parse the number */
  939. result = ufmt_uto64(input->str.fPos, &len, 8);
  940. /* update the input's position to reflect consumed data */
  941. input->str.fPos += len;
  942. /* mask off any necessary bits */
  943. if (!info->fSkipArg) {
  944. if (info->fIsShort)
  945. *static_cast<int16_t*>(num) = static_cast<int16_t>(UINT16_MAX & result);
  946. else if (info->fIsLongLong)
  947. *static_cast<int64_t*>(num) = result;
  948. else
  949. *static_cast<int32_t*>(num) = static_cast<int32_t>(UINT32_MAX & result);
  950. }
  951. /* we converted 1 arg */
  952. *argConverted = !info->fSkipArg;
  953. return len + skipped;
  954. }
  955. static int32_t
  956. u_scanf_pointer_handler(UFILE *input,
  957. u_scanf_spec_info *info,
  958. ufmt_args *args,
  959. const char16_t *fmt,
  960. int32_t *fmtConsumed,
  961. int32_t *argConverted)
  962. {
  963. (void)fmt;
  964. (void)fmtConsumed;
  965. int32_t len;
  966. int32_t skipped;
  967. void *result;
  968. void** p = static_cast<void**>(args[0].ptrValue);
  969. /* skip all ws in the input */
  970. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  971. /* fill the input's internal buffer */
  972. ufile_fill_uchar_buffer(input);
  973. /* determine the size of the input's buffer */
  974. len = static_cast<int32_t>(input->str.fLimit - input->str.fPos);
  975. /* truncate to the width, if specified */
  976. if(info->fWidth != -1) {
  977. len = ufmt_min(len, info->fWidth);
  978. }
  979. /* Make sure that we don't consume too much */
  980. if (len > static_cast<int32_t>(sizeof(void*) * 2)) {
  981. len = static_cast<int32_t>(sizeof(void*) * 2);
  982. }
  983. /* parse the pointer - assign to temporary value */
  984. result = ufmt_utop(input->str.fPos, &len);
  985. if (!info->fSkipArg) {
  986. *p = result;
  987. }
  988. /* update the input's position to reflect consumed data */
  989. input->str.fPos += len;
  990. /* we converted 1 arg */
  991. *argConverted = !info->fSkipArg;
  992. return len + skipped;
  993. }
  994. static int32_t
  995. u_scanf_scanset_handler(UFILE *input,
  996. u_scanf_spec_info *info,
  997. ufmt_args *args,
  998. const char16_t *fmt,
  999. int32_t *fmtConsumed,
  1000. int32_t *argConverted)
  1001. {
  1002. USet *scanset;
  1003. UErrorCode status = U_ZERO_ERROR;
  1004. int32_t chLeft = INT32_MAX;
  1005. UChar32 c;
  1006. char16_t* alias = static_cast<char16_t*>(args[0].ptrValue);
  1007. UBool isNotEOF = false;
  1008. UBool readCharacter = false;
  1009. /* Create an empty set */
  1010. scanset = uset_open(0, -1);
  1011. /* Back up one to get the [ */
  1012. fmt--;
  1013. /* truncate to the width, if specified and alias the target */
  1014. if(info->fWidth >= 0) {
  1015. chLeft = info->fWidth;
  1016. }
  1017. /* parse the scanset from the fmt string */
  1018. *fmtConsumed = uset_applyPattern(scanset, fmt, -1, 0, &status);
  1019. /* verify that the parse was successful */
  1020. if (U_SUCCESS(status)) {
  1021. c=0;
  1022. /* grab characters one at a time and make sure they are in the scanset */
  1023. while(chLeft > 0) {
  1024. if (((isNotEOF = ufile_getch32(input, &c)) == static_cast<UBool>(true)) && uset_contains(scanset, c)) {
  1025. readCharacter = true;
  1026. if (!info->fSkipArg) {
  1027. int32_t idx = 0;
  1028. UBool isError = false;
  1029. U16_APPEND(alias, idx, chLeft, c, isError);
  1030. if (isError) {
  1031. break;
  1032. }
  1033. alias += idx;
  1034. }
  1035. chLeft -= (1 + U_IS_SUPPLEMENTARY(c));
  1036. }
  1037. else {
  1038. /* if the character's not in the scanset, break out */
  1039. break;
  1040. }
  1041. }
  1042. /* put the final character we read back on the input */
  1043. if(isNotEOF && chLeft > 0) {
  1044. u_fungetc(c, input);
  1045. }
  1046. }
  1047. uset_close(scanset);
  1048. /* if we didn't match at least 1 character, fail */
  1049. if(!readCharacter)
  1050. return -1;
  1051. /* otherwise, add the terminator */
  1052. else if (!info->fSkipArg) {
  1053. *alias = 0x00;
  1054. }
  1055. /* we converted 1 arg */
  1056. *argConverted = !info->fSkipArg;
  1057. return (info->fWidth >= 0 ? info->fWidth : INT32_MAX) - chLeft;
  1058. }
  1059. /* Use US-ASCII characters only for formatting. Most codepages have
  1060. characters 20-7F from Unicode. Using any other codepage specific
  1061. characters will make it very difficult to format the string on
  1062. non-Unicode machines */
  1063. static const u_scanf_info g_u_scanf_infos[USCANF_NUM_FMT_HANDLERS] = {
  1064. /* 0x20 */
  1065. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1066. UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY,
  1067. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1068. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1069. /* 0x30 */
  1070. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1071. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1072. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1073. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1074. /* 0x40 */
  1075. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR,
  1076. UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL,
  1077. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  1078. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/,
  1079. #else
  1080. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1081. #endif
  1082. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1083. /* 0x50 */
  1084. UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING,
  1085. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  1086. UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY,
  1087. #else
  1088. UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY,
  1089. #endif
  1090. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_SCANSET,
  1091. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1092. /* 0x60 */
  1093. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR,
  1094. UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL,
  1095. UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY,
  1096. UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL,
  1097. /* 0x70 */
  1098. UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING,
  1099. UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY,
  1100. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1101. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1102. };
  1103. U_CFUNC int32_t
  1104. u_scanf_parse(UFILE *f,
  1105. const char16_t *patternSpecification,
  1106. va_list ap)
  1107. {
  1108. const char16_t *alias;
  1109. int32_t count, converted, argConsumed, cpConsumed;
  1110. uint16_t handlerNum;
  1111. ufmt_args args;
  1112. u_scanf_spec spec;
  1113. ufmt_type_info info;
  1114. u_scanf_handler handler;
  1115. /* alias the pattern */
  1116. alias = patternSpecification;
  1117. /* haven't converted anything yet */
  1118. argConsumed = 0;
  1119. converted = 0;
  1120. cpConsumed = 0;
  1121. /* iterate through the pattern */
  1122. for(;;) {
  1123. /* match any characters up to the next '%' */
  1124. while(*alias != UP_PERCENT && *alias != 0x0000 && u_fgetc(f) == *alias) {
  1125. alias++;
  1126. }
  1127. /* if we aren't at a '%', or if we're at end of string, break*/
  1128. if(*alias != UP_PERCENT || *alias == 0x0000)
  1129. break;
  1130. /* parse the specifier */
  1131. count = u_scanf_parse_spec(alias, &spec);
  1132. /* update the pointer in pattern */
  1133. alias += count;
  1134. handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS);
  1135. if (handlerNum < USCANF_NUM_FMT_HANDLERS) {
  1136. /* skip the argument, if necessary */
  1137. /* query the info function for argument information */
  1138. info = g_u_scanf_infos[ handlerNum ].info;
  1139. if (info != ufmt_count && u_feof(f)) {
  1140. break;
  1141. }
  1142. else if(spec.fInfo.fSkipArg) {
  1143. args.ptrValue = nullptr;
  1144. }
  1145. else {
  1146. switch(info) {
  1147. case ufmt_count:
  1148. /* set the spec's width to the # of items converted */
  1149. spec.fInfo.fWidth = cpConsumed;
  1150. U_FALLTHROUGH;
  1151. case ufmt_char:
  1152. case ufmt_uchar:
  1153. case ufmt_int:
  1154. case ufmt_string:
  1155. case ufmt_ustring:
  1156. case ufmt_pointer:
  1157. case ufmt_float:
  1158. case ufmt_double:
  1159. args.ptrValue = va_arg(ap, void*);
  1160. break;
  1161. default:
  1162. /* else args is ignored */
  1163. args.ptrValue = nullptr;
  1164. break;
  1165. }
  1166. }
  1167. /* call the handler function */
  1168. handler = g_u_scanf_infos[ handlerNum ].handler;
  1169. if (handler != nullptr) {
  1170. /* reset count to 1 so that += for alias works. */
  1171. count = 1;
  1172. cpConsumed += (*handler)(f, &spec.fInfo, &args, alias, &count, &argConsumed);
  1173. /* if the handler encountered an error condition, break */
  1174. if(argConsumed < 0) {
  1175. converted = -1;
  1176. break;
  1177. }
  1178. /* add to the # of items converted */
  1179. converted += argConsumed;
  1180. /* update the pointer in pattern */
  1181. alias += count-1;
  1182. }
  1183. /* else do nothing */
  1184. }
  1185. /* else do nothing */
  1186. /* just ignore unknown tags */
  1187. }
  1188. /* return # of items converted */
  1189. return converted;
  1190. }
  1191. #endif /* #if !UCONFIG_NO_FORMATTING */