formatter.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. package tengo
  2. import (
  3. "strconv"
  4. "sync"
  5. "unicode/utf8"
  6. )
  7. // Strings for use with fmtbuf.WriteString. This is less overhead than using
  8. // fmtbuf.Write with byte arrays.
  9. const (
  10. commaSpaceString = ", "
  11. nilParenString = "(nil)"
  12. percentBangString = "%!"
  13. missingString = "(MISSING)"
  14. badIndexString = "(BADINDEX)"
  15. extraString = "%!(EXTRA "
  16. badWidthString = "%!(BADWIDTH)"
  17. badPrecString = "%!(BADPREC)"
  18. noVerbString = "%!(NOVERB)"
  19. )
  20. const (
  21. ldigits = "0123456789abcdefx"
  22. udigits = "0123456789ABCDEFX"
  23. )
  24. const (
  25. signed = true
  26. unsigned = false
  27. )
  28. // flags placed in a separate struct for easy clearing.
  29. type fmtFlags struct {
  30. widPresent bool
  31. precPresent bool
  32. minus bool
  33. plus bool
  34. sharp bool
  35. space bool
  36. zero bool
  37. // For the formats %+v %#v, we set the plusV/sharpV flags
  38. // and clear the plus/sharp flags since %+v and %#v are in effect
  39. // different, flagless formats set at the top level.
  40. plusV bool
  41. sharpV bool
  42. // error-related flags.
  43. inDetail bool
  44. needNewline bool
  45. needColon bool
  46. }
  47. // A formatter is the raw formatter used by Printf etc.
  48. // It prints into a fmtbuf that must be set up separately.
  49. type formatter struct {
  50. buf *fmtbuf
  51. fmtFlags
  52. wid int // width
  53. prec int // precision
  54. // intbuf is large enough to store %b of an int64 with a sign and
  55. // avoids padding at the end of the struct on 32 bit architectures.
  56. intbuf [68]byte
  57. }
  58. func (f *formatter) clearFlags() {
  59. f.fmtFlags = fmtFlags{}
  60. }
  61. func (f *formatter) init(buf *fmtbuf) {
  62. f.buf = buf
  63. f.clearFlags()
  64. }
  65. // writePadding generates n bytes of padding.
  66. func (f *formatter) writePadding(n int) {
  67. if n <= 0 { // No padding bytes needed.
  68. return
  69. }
  70. buf := *f.buf
  71. oldLen := len(buf)
  72. newLen := oldLen + n
  73. if newLen > MaxStringLen {
  74. panic(ErrStringLimit)
  75. }
  76. // Make enough room for padding.
  77. if newLen > cap(buf) {
  78. buf = make(fmtbuf, cap(buf)*2+n)
  79. copy(buf, *f.buf)
  80. }
  81. // Decide which byte the padding should be filled with.
  82. padByte := byte(' ')
  83. if f.zero {
  84. padByte = byte('0')
  85. }
  86. // Fill padding with padByte.
  87. padding := buf[oldLen:newLen]
  88. for i := range padding {
  89. padding[i] = padByte
  90. }
  91. *f.buf = buf[:newLen]
  92. }
  93. // pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
  94. func (f *formatter) pad(b []byte) {
  95. if !f.widPresent || f.wid == 0 {
  96. f.buf.Write(b)
  97. return
  98. }
  99. width := f.wid - utf8.RuneCount(b)
  100. if !f.minus {
  101. // left padding
  102. f.writePadding(width)
  103. f.buf.Write(b)
  104. } else {
  105. // right padding
  106. f.buf.Write(b)
  107. f.writePadding(width)
  108. }
  109. }
  110. // padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
  111. func (f *formatter) padString(s string) {
  112. if !f.widPresent || f.wid == 0 {
  113. f.buf.WriteString(s)
  114. return
  115. }
  116. width := f.wid - utf8.RuneCountInString(s)
  117. if !f.minus {
  118. // left padding
  119. f.writePadding(width)
  120. f.buf.WriteString(s)
  121. } else {
  122. // right padding
  123. f.buf.WriteString(s)
  124. f.writePadding(width)
  125. }
  126. }
  127. // fmtBoolean formats a boolean.
  128. func (f *formatter) fmtBoolean(v bool) {
  129. if v {
  130. f.padString("true")
  131. } else {
  132. f.padString("false")
  133. }
  134. }
  135. // fmtUnicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'".
  136. func (f *formatter) fmtUnicode(u uint64) {
  137. buf := f.intbuf[0:]
  138. // With default precision set the maximum needed buf length is 18
  139. // for formatting -1 with %#U ("U+FFFFFFFFFFFFFFFF") which fits
  140. // into the already allocated intbuf with a capacity of 68 bytes.
  141. prec := 4
  142. if f.precPresent && f.prec > 4 {
  143. prec = f.prec
  144. // Compute space needed for "U+" , number, " '", character, "'".
  145. width := 2 + prec + 2 + utf8.UTFMax + 1
  146. if width > len(buf) {
  147. buf = make([]byte, width)
  148. }
  149. }
  150. // Format into buf, ending at buf[i]. Formatting numbers is easier
  151. // right-to-left.
  152. i := len(buf)
  153. // For %#U we want to add a space and a quoted character at the end of
  154. // the fmtbuf.
  155. if f.sharp && u <= utf8.MaxRune && strconv.IsPrint(rune(u)) {
  156. i--
  157. buf[i] = '\''
  158. i -= utf8.RuneLen(rune(u))
  159. utf8.EncodeRune(buf[i:], rune(u))
  160. i--
  161. buf[i] = '\''
  162. i--
  163. buf[i] = ' '
  164. }
  165. // Format the Unicode code point u as a hexadecimal number.
  166. for u >= 16 {
  167. i--
  168. buf[i] = udigits[u&0xF]
  169. prec--
  170. u >>= 4
  171. }
  172. i--
  173. buf[i] = udigits[u]
  174. prec--
  175. // Add zeros in front of the number until requested precision is reached.
  176. for prec > 0 {
  177. i--
  178. buf[i] = '0'
  179. prec--
  180. }
  181. // Add a leading "U+".
  182. i--
  183. buf[i] = '+'
  184. i--
  185. buf[i] = 'U'
  186. oldZero := f.zero
  187. f.zero = false
  188. f.pad(buf[i:])
  189. f.zero = oldZero
  190. }
  191. // fmtInteger formats signed and unsigned integers.
  192. func (f *formatter) fmtInteger(
  193. u uint64,
  194. base int,
  195. isSigned bool,
  196. verb rune,
  197. digits string,
  198. ) {
  199. negative := isSigned && int64(u) < 0
  200. if negative {
  201. u = -u
  202. }
  203. buf := f.intbuf[0:]
  204. // The already allocated f.intbuf with a capacity of 68 bytes
  205. // is large enough for integer formatting when no precision or width is set.
  206. if f.widPresent || f.precPresent {
  207. // Account 3 extra bytes for possible addition of a sign and "0x".
  208. width := 3 + f.wid + f.prec // wid and prec are always positive.
  209. if width > len(buf) {
  210. // We're going to need a bigger boat.
  211. buf = make([]byte, width)
  212. }
  213. }
  214. // Two ways to ask for extra leading zero digits: %.3d or %03d.
  215. // If both are specified the f.zero flag is ignored and
  216. // padding with spaces is used instead.
  217. prec := 0
  218. if f.precPresent {
  219. prec = f.prec
  220. // Precision of 0 and value of 0 means "print nothing" but padding.
  221. if prec == 0 && u == 0 {
  222. oldZero := f.zero
  223. f.zero = false
  224. f.writePadding(f.wid)
  225. f.zero = oldZero
  226. return
  227. }
  228. } else if f.zero && f.widPresent {
  229. prec = f.wid
  230. if negative || f.plus || f.space {
  231. prec-- // leave room for sign
  232. }
  233. }
  234. // Because printing is easier right-to-left: format u into buf, ending at
  235. // buf[i]. We could make things marginally faster by splitting the 32-bit
  236. // case out into a separate block but it's not worth the duplication, so
  237. // u has 64 bits.
  238. i := len(buf)
  239. // Use constants for the division and modulo for more efficient code.
  240. // Switch cases ordered by popularity.
  241. switch base {
  242. case 10:
  243. for u >= 10 {
  244. i--
  245. next := u / 10
  246. buf[i] = byte('0' + u - next*10)
  247. u = next
  248. }
  249. case 16:
  250. for u >= 16 {
  251. i--
  252. buf[i] = digits[u&0xF]
  253. u >>= 4
  254. }
  255. case 8:
  256. for u >= 8 {
  257. i--
  258. buf[i] = byte('0' + u&7)
  259. u >>= 3
  260. }
  261. case 2:
  262. for u >= 2 {
  263. i--
  264. buf[i] = byte('0' + u&1)
  265. u >>= 1
  266. }
  267. default:
  268. panic("fmt: unknown base; can't happen")
  269. }
  270. i--
  271. buf[i] = digits[u]
  272. for i > 0 && prec > len(buf)-i {
  273. i--
  274. buf[i] = '0'
  275. }
  276. // Various prefixes: 0x, -, etc.
  277. if f.sharp {
  278. switch base {
  279. case 2:
  280. // Add a leading 0b.
  281. i--
  282. buf[i] = 'b'
  283. i--
  284. buf[i] = '0'
  285. case 8:
  286. if buf[i] != '0' {
  287. i--
  288. buf[i] = '0'
  289. }
  290. case 16:
  291. // Add a leading 0x or 0X.
  292. i--
  293. buf[i] = digits[16]
  294. i--
  295. buf[i] = '0'
  296. }
  297. }
  298. if verb == 'O' {
  299. i--
  300. buf[i] = 'o'
  301. i--
  302. buf[i] = '0'
  303. }
  304. if negative {
  305. i--
  306. buf[i] = '-'
  307. } else if f.plus {
  308. i--
  309. buf[i] = '+'
  310. } else if f.space {
  311. i--
  312. buf[i] = ' '
  313. }
  314. // Left padding with zeros has already been handled like precision earlier
  315. // or the f.zero flag is ignored due to an explicitly set precision.
  316. oldZero := f.zero
  317. f.zero = false
  318. f.pad(buf[i:])
  319. f.zero = oldZero
  320. }
  321. // truncate truncates the string s to the specified precision, if present.
  322. func (f *formatter) truncateString(s string) string {
  323. if f.precPresent {
  324. n := f.prec
  325. for i := range s {
  326. n--
  327. if n < 0 {
  328. return s[:i]
  329. }
  330. }
  331. }
  332. return s
  333. }
  334. // truncate truncates the byte slice b as a string of the specified precision,
  335. // if present.
  336. func (f *formatter) truncate(b []byte) []byte {
  337. if f.precPresent {
  338. n := f.prec
  339. for i := 0; i < len(b); {
  340. n--
  341. if n < 0 {
  342. return b[:i]
  343. }
  344. wid := 1
  345. if b[i] >= utf8.RuneSelf {
  346. _, wid = utf8.DecodeRune(b[i:])
  347. }
  348. i += wid
  349. }
  350. }
  351. return b
  352. }
  353. // fmtS formats a string.
  354. func (f *formatter) fmtS(s string) {
  355. s = f.truncateString(s)
  356. f.padString(s)
  357. }
  358. // fmtBs formats the byte slice b as if it was formatted as string with fmtS.
  359. func (f *formatter) fmtBs(b []byte) {
  360. b = f.truncate(b)
  361. f.pad(b)
  362. }
  363. // fmtSbx formats a string or byte slice as a hexadecimal encoding of its bytes.
  364. func (f *formatter) fmtSbx(s string, b []byte, digits string) {
  365. length := len(b)
  366. if b == nil {
  367. // No byte slice present. Assume string s should be encoded.
  368. length = len(s)
  369. }
  370. // Set length to not process more bytes than the precision demands.
  371. if f.precPresent && f.prec < length {
  372. length = f.prec
  373. }
  374. // Compute width of the encoding taking into account the f.sharp and
  375. // f.space flag.
  376. width := 2 * length
  377. if width > 0 {
  378. if f.space {
  379. // Each element encoded by two hexadecimals will get a leading
  380. // 0x or 0X.
  381. if f.sharp {
  382. width *= 2
  383. }
  384. // Elements will be separated by a space.
  385. width += length - 1
  386. } else if f.sharp {
  387. // Only a leading 0x or 0X will be added for the whole string.
  388. width += 2
  389. }
  390. } else { // The byte slice or string that should be encoded is empty.
  391. if f.widPresent {
  392. f.writePadding(f.wid)
  393. }
  394. return
  395. }
  396. // Handle padding to the left.
  397. if f.widPresent && f.wid > width && !f.minus {
  398. f.writePadding(f.wid - width)
  399. }
  400. // Write the encoding directly into the output fmtbuf.
  401. buf := *f.buf
  402. if f.sharp {
  403. // Add leading 0x or 0X.
  404. buf = append(buf, '0', digits[16])
  405. }
  406. var c byte
  407. for i := 0; i < length; i++ {
  408. if f.space && i > 0 {
  409. // Separate elements with a space.
  410. buf = append(buf, ' ')
  411. if f.sharp {
  412. // Add leading 0x or 0X for each element.
  413. buf = append(buf, '0', digits[16])
  414. }
  415. }
  416. if b != nil {
  417. c = b[i] // Take a byte from the input byte slice.
  418. } else {
  419. c = s[i] // Take a byte from the input string.
  420. }
  421. // Encode each byte as two hexadecimal digits.
  422. buf = append(buf, digits[c>>4], digits[c&0xF])
  423. }
  424. *f.buf = buf
  425. // Handle padding to the right.
  426. if f.widPresent && f.wid > width && f.minus {
  427. f.writePadding(f.wid - width)
  428. }
  429. }
  430. // fmtSx formats a string as a hexadecimal encoding of its bytes.
  431. func (f *formatter) fmtSx(s, digits string) {
  432. f.fmtSbx(s, nil, digits)
  433. }
  434. // fmtBx formats a byte slice as a hexadecimal encoding of its bytes.
  435. func (f *formatter) fmtBx(b []byte, digits string) {
  436. f.fmtSbx("", b, digits)
  437. }
  438. // fmtQ formats a string as a double-quoted, escaped Go string constant.
  439. // If f.sharp is set a raw (backquoted) string may be returned instead
  440. // if the string does not contain any control characters other than tab.
  441. func (f *formatter) fmtQ(s string) {
  442. s = f.truncateString(s)
  443. if f.sharp && strconv.CanBackquote(s) {
  444. f.padString("`" + s + "`")
  445. return
  446. }
  447. buf := f.intbuf[:0]
  448. if f.plus {
  449. f.pad(strconv.AppendQuoteToASCII(buf, s))
  450. } else {
  451. f.pad(strconv.AppendQuote(buf, s))
  452. }
  453. }
  454. // fmtC formats an integer as a Unicode character.
  455. // If the character is not valid Unicode, it will print '\ufffd'.
  456. func (f *formatter) fmtC(c uint64) {
  457. r := rune(c)
  458. if c > utf8.MaxRune {
  459. r = utf8.RuneError
  460. }
  461. buf := f.intbuf[:0]
  462. w := utf8.EncodeRune(buf[:utf8.UTFMax], r)
  463. f.pad(buf[:w])
  464. }
  465. // fmtQc formats an integer as a single-quoted, escaped Go character constant.
  466. // If the character is not valid Unicode, it will print '\ufffd'.
  467. func (f *formatter) fmtQc(c uint64) {
  468. r := rune(c)
  469. if c > utf8.MaxRune {
  470. r = utf8.RuneError
  471. }
  472. buf := f.intbuf[:0]
  473. if f.plus {
  474. f.pad(strconv.AppendQuoteRuneToASCII(buf, r))
  475. } else {
  476. f.pad(strconv.AppendQuoteRune(buf, r))
  477. }
  478. }
  479. // fmtFloat formats a float64. It assumes that verb is a valid format specifier
  480. // for strconv.AppendFloat and therefore fits into a byte.
  481. func (f *formatter) fmtFloat(v float64, size int, verb rune, prec int) {
  482. // Explicit precision in format specifier overrules default precision.
  483. if f.precPresent {
  484. prec = f.prec
  485. }
  486. // Format number, reserving space for leading + sign if needed.
  487. num := strconv.AppendFloat(f.intbuf[:1], v, byte(verb), prec, size)
  488. if num[1] == '-' || num[1] == '+' {
  489. num = num[1:]
  490. } else {
  491. num[0] = '+'
  492. }
  493. // f.space means to add a leading space instead of a "+" sign unless
  494. // the sign is explicitly asked for by f.plus.
  495. if f.space && num[0] == '+' && !f.plus {
  496. num[0] = ' '
  497. }
  498. // Special handling for infinities and NaN,
  499. // which don't look like a number so shouldn't be padded with zeros.
  500. if num[1] == 'I' || num[1] == 'N' {
  501. oldZero := f.zero
  502. f.zero = false
  503. // Remove sign before NaN if not asked for.
  504. if num[1] == 'N' && !f.space && !f.plus {
  505. num = num[1:]
  506. }
  507. f.pad(num)
  508. f.zero = oldZero
  509. return
  510. }
  511. // The sharp flag forces printing a decimal point for non-binary formats
  512. // and retains trailing zeros, which we may need to restore.
  513. if f.sharp && verb != 'b' {
  514. digits := 0
  515. switch verb {
  516. case 'v', 'g', 'G', 'x':
  517. digits = prec
  518. // If no precision is set explicitly use a precision of 6.
  519. if digits == -1 {
  520. digits = 6
  521. }
  522. }
  523. // Buffer pre-allocated with enough room for
  524. // exponent notations of the form "e+123" or "p-1023".
  525. var tailBuf [6]byte
  526. tail := tailBuf[:0]
  527. hasDecimalPoint := false
  528. // Starting from i = 1 to skip sign at num[0].
  529. for i := 1; i < len(num); i++ {
  530. switch num[i] {
  531. case '.':
  532. hasDecimalPoint = true
  533. case 'p', 'P':
  534. tail = append(tail, num[i:]...)
  535. num = num[:i]
  536. case 'e', 'E':
  537. if verb != 'x' && verb != 'X' {
  538. tail = append(tail, num[i:]...)
  539. num = num[:i]
  540. break
  541. }
  542. fallthrough
  543. default:
  544. digits--
  545. }
  546. }
  547. if !hasDecimalPoint {
  548. num = append(num, '.')
  549. }
  550. for digits > 0 {
  551. num = append(num, '0')
  552. digits--
  553. }
  554. num = append(num, tail...)
  555. }
  556. // We want a sign if asked for and if the sign is not positive.
  557. if f.plus || num[0] != '+' {
  558. // If we're zero padding to the left we want the sign before the
  559. // leading zeros. Achieve this by writing the sign out and then padding
  560. // the unsigned number.
  561. if f.zero && f.widPresent && f.wid > len(num) {
  562. f.buf.WriteSingleByte(num[0])
  563. f.writePadding(f.wid - len(num))
  564. f.buf.Write(num[1:])
  565. return
  566. }
  567. f.pad(num)
  568. return
  569. }
  570. // No sign to show and the number is positive; just print the unsigned
  571. // number.
  572. f.pad(num[1:])
  573. }
  574. // Use simple []byte instead of bytes.Buffer to avoid large dependency.
  575. type fmtbuf []byte
  576. func (b *fmtbuf) Write(p []byte) {
  577. if len(*b)+len(p) > MaxStringLen {
  578. panic(ErrStringLimit)
  579. }
  580. *b = append(*b, p...)
  581. }
  582. func (b *fmtbuf) WriteString(s string) {
  583. if len(*b)+len(s) > MaxStringLen {
  584. panic(ErrStringLimit)
  585. }
  586. *b = append(*b, s...)
  587. }
  588. func (b *fmtbuf) WriteSingleByte(c byte) {
  589. if len(*b) >= MaxStringLen {
  590. panic(ErrStringLimit)
  591. }
  592. *b = append(*b, c)
  593. }
  594. func (b *fmtbuf) WriteRune(r rune) {
  595. if len(*b)+utf8.RuneLen(r) > MaxStringLen {
  596. panic(ErrStringLimit)
  597. }
  598. if r < utf8.RuneSelf {
  599. *b = append(*b, byte(r))
  600. return
  601. }
  602. b2 := *b
  603. n := len(b2)
  604. for n+utf8.UTFMax > cap(b2) {
  605. b2 = append(b2, 0)
  606. }
  607. w := utf8.EncodeRune(b2[n:n+utf8.UTFMax], r)
  608. *b = b2[:n+w]
  609. }
  610. // pp is used to store a printer's state and is reused with sync.Pool to avoid
  611. // allocations.
  612. type pp struct {
  613. buf fmtbuf
  614. // arg holds the current item.
  615. arg Object
  616. // fmt is used to format basic items such as integers or strings.
  617. fmt formatter
  618. // reordered records whether the format string used argument reordering.
  619. reordered bool
  620. // goodArgNum records whether the most recent reordering directive was
  621. // valid.
  622. goodArgNum bool
  623. // erroring is set when printing an error string to guard against calling
  624. // handleMethods.
  625. erroring bool
  626. }
  627. var ppFree = sync.Pool{
  628. New: func() interface{} { return new(pp) },
  629. }
  630. // newPrinter allocates a new pp struct or grabs a cached one.
  631. func newPrinter() *pp {
  632. p := ppFree.Get().(*pp)
  633. p.erroring = false
  634. p.fmt.init(&p.buf)
  635. return p
  636. }
  637. // free saves used pp structs in ppFree; avoids an allocation per invocation.
  638. func (p *pp) free() {
  639. // Proper usage of a sync.Pool requires each entry to have approximately
  640. // the same memory cost. To obtain this property when the stored type
  641. // contains a variably-sized fmtbuf, we add a hard limit on the maximum
  642. // fmtbuf to place back in the pool.
  643. //
  644. // See https://golang.org/issue/23199
  645. if cap(p.buf) > 64<<10 {
  646. return
  647. }
  648. p.buf = p.buf[:0]
  649. p.arg = nil
  650. ppFree.Put(p)
  651. }
  652. func (p *pp) Width() (wid int, ok bool) {
  653. return p.fmt.wid, p.fmt.widPresent
  654. }
  655. func (p *pp) Precision() (prec int, ok bool) {
  656. return p.fmt.prec, p.fmt.precPresent
  657. }
  658. func (p *pp) Flag(b int) bool {
  659. switch b {
  660. case '-':
  661. return p.fmt.minus
  662. case '+':
  663. return p.fmt.plus || p.fmt.plusV
  664. case '#':
  665. return p.fmt.sharp || p.fmt.sharpV
  666. case ' ':
  667. return p.fmt.space
  668. case '0':
  669. return p.fmt.zero
  670. }
  671. return false
  672. }
  673. // Implement Write so we can call Fprintf on a pp (through State), for
  674. // recursive use in custom verbs.
  675. func (p *pp) Write(b []byte) (ret int, err error) {
  676. p.buf.Write(b)
  677. return len(b), nil
  678. }
  679. // Implement WriteString so that we can call io.WriteString
  680. // on a pp (through state), for efficiency.
  681. func (p *pp) WriteString(s string) (ret int, err error) {
  682. p.buf.WriteString(s)
  683. return len(s), nil
  684. }
  685. func (p *pp) WriteRune(r rune) (ret int, err error) {
  686. p.buf.WriteRune(r)
  687. return utf8.RuneLen(r), nil
  688. }
  689. func (p *pp) WriteSingleByte(c byte) (ret int, err error) {
  690. p.buf.WriteSingleByte(c)
  691. return 1, nil
  692. }
  693. // tooLarge reports whether the magnitude of the integer is
  694. // too large to be used as a formatting width or precision.
  695. func tooLarge(x int) bool {
  696. const max int = 1e6
  697. return x > max || x < -max
  698. }
  699. // parsenum converts ASCII to integer. num is 0 (and isnum is false) if no
  700. // number present.
  701. func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
  702. if start >= end {
  703. return 0, false, end
  704. }
  705. for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ {
  706. if tooLarge(num) {
  707. return 0, false, end // Overflow; crazy long number most likely.
  708. }
  709. num = num*10 + int(s[newi]-'0')
  710. isnum = true
  711. }
  712. return
  713. }
  714. func (p *pp) badVerb(verb rune) {
  715. p.erroring = true
  716. _, _ = p.WriteString(percentBangString)
  717. _, _ = p.WriteRune(verb)
  718. _, _ = p.WriteSingleByte('(')
  719. switch {
  720. case p.arg != nil:
  721. _, _ = p.WriteString(p.arg.String())
  722. _, _ = p.WriteSingleByte('=')
  723. p.printArg(p.arg, 'v')
  724. default:
  725. _, _ = p.WriteString(UndefinedValue.String())
  726. }
  727. _, _ = p.WriteSingleByte(')')
  728. p.erroring = false
  729. }
  730. func (p *pp) fmtBool(v bool, verb rune) {
  731. switch verb {
  732. case 't', 'v':
  733. p.fmt.fmtBoolean(v)
  734. default:
  735. p.badVerb(verb)
  736. }
  737. }
  738. // fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
  739. // not, as requested, by temporarily setting the sharp flag.
  740. func (p *pp) fmt0x64(v uint64, leading0x bool) {
  741. sharp := p.fmt.sharp
  742. p.fmt.sharp = leading0x
  743. p.fmt.fmtInteger(v, 16, unsigned, 'v', ldigits)
  744. p.fmt.sharp = sharp
  745. }
  746. // fmtInteger formats a signed or unsigned integer.
  747. func (p *pp) fmtInteger(v uint64, isSigned bool, verb rune) {
  748. switch verb {
  749. case 'v':
  750. if p.fmt.sharpV && !isSigned {
  751. p.fmt0x64(v, true)
  752. } else {
  753. p.fmt.fmtInteger(v, 10, isSigned, verb, ldigits)
  754. }
  755. case 'd':
  756. p.fmt.fmtInteger(v, 10, isSigned, verb, ldigits)
  757. case 'b':
  758. p.fmt.fmtInteger(v, 2, isSigned, verb, ldigits)
  759. case 'o', 'O':
  760. p.fmt.fmtInteger(v, 8, isSigned, verb, ldigits)
  761. case 'x':
  762. p.fmt.fmtInteger(v, 16, isSigned, verb, ldigits)
  763. case 'X':
  764. p.fmt.fmtInteger(v, 16, isSigned, verb, udigits)
  765. case 'c':
  766. p.fmt.fmtC(v)
  767. case 'q':
  768. if v <= utf8.MaxRune {
  769. p.fmt.fmtQc(v)
  770. } else {
  771. p.badVerb(verb)
  772. }
  773. case 'U':
  774. p.fmt.fmtUnicode(v)
  775. default:
  776. p.badVerb(verb)
  777. }
  778. }
  779. // fmtFloat formats a float. The default precision for each verb
  780. // is specified as last argument in the call to fmt_float.
  781. func (p *pp) fmtFloat(v float64, size int, verb rune) {
  782. switch verb {
  783. case 'v':
  784. p.fmt.fmtFloat(v, size, 'g', -1)
  785. case 'b', 'g', 'G', 'x', 'X':
  786. p.fmt.fmtFloat(v, size, verb, -1)
  787. case 'f', 'e', 'E':
  788. p.fmt.fmtFloat(v, size, verb, 6)
  789. case 'F':
  790. p.fmt.fmtFloat(v, size, 'f', 6)
  791. default:
  792. p.badVerb(verb)
  793. }
  794. }
  795. func (p *pp) fmtString(v string, verb rune) {
  796. switch verb {
  797. case 'v':
  798. if p.fmt.sharpV {
  799. p.fmt.fmtQ(v)
  800. } else {
  801. p.fmt.fmtS(v)
  802. }
  803. case 's':
  804. p.fmt.fmtS(v)
  805. case 'x':
  806. p.fmt.fmtSx(v, ldigits)
  807. case 'X':
  808. p.fmt.fmtSx(v, udigits)
  809. case 'q':
  810. p.fmt.fmtQ(v)
  811. default:
  812. p.badVerb(verb)
  813. }
  814. }
  815. func (p *pp) fmtBytes(v []byte, verb rune, typeString string) {
  816. switch verb {
  817. case 'v', 'd':
  818. if p.fmt.sharpV {
  819. _, _ = p.WriteString(typeString)
  820. if v == nil {
  821. _, _ = p.WriteString(nilParenString)
  822. return
  823. }
  824. _, _ = p.WriteSingleByte('{')
  825. for i, c := range v {
  826. if i > 0 {
  827. _, _ = p.WriteString(commaSpaceString)
  828. }
  829. p.fmt0x64(uint64(c), true)
  830. }
  831. _, _ = p.WriteSingleByte('}')
  832. } else {
  833. _, _ = p.WriteSingleByte('[')
  834. for i, c := range v {
  835. if i > 0 {
  836. _, _ = p.WriteSingleByte(' ')
  837. }
  838. p.fmt.fmtInteger(uint64(c), 10, unsigned, verb, ldigits)
  839. }
  840. _, _ = p.WriteSingleByte(']')
  841. }
  842. case 's':
  843. p.fmt.fmtBs(v)
  844. case 'x':
  845. p.fmt.fmtBx(v, ldigits)
  846. case 'X':
  847. p.fmt.fmtBx(v, udigits)
  848. case 'q':
  849. p.fmt.fmtQ(string(v))
  850. }
  851. }
  852. func (p *pp) printArg(arg Object, verb rune) {
  853. p.arg = arg
  854. if arg == nil {
  855. arg = UndefinedValue
  856. }
  857. // Special processing considerations.
  858. // %T (the value's type) and %p (its address) are special; we always do
  859. // them first.
  860. switch verb {
  861. case 'T':
  862. p.fmt.fmtS(arg.TypeName())
  863. return
  864. case 'v':
  865. p.fmt.fmtS(arg.String())
  866. return
  867. }
  868. // Some types can be done without reflection.
  869. switch f := arg.(type) {
  870. case Bool:
  871. p.fmtBool(!f.IsFalsy(), verb)
  872. case Float:
  873. p.fmtFloat(f.Value, 64, verb)
  874. case Int:
  875. p.fmtInteger(uint64(f.Value), signed, verb)
  876. case *String:
  877. p.fmtString(f.Value, verb)
  878. case *Bytes:
  879. p.fmtBytes(f.Value, verb, "[]byte")
  880. default:
  881. p.fmtString(f.String(), verb)
  882. }
  883. }
  884. // intFromArg gets the argNumth element of a. On return, isInt reports whether
  885. // the argument has integer type.
  886. func intFromArg(a []Object, argNum int) (num int, isInt bool, newArgNum int) {
  887. newArgNum = argNum
  888. if argNum < len(a) {
  889. var num64 int64
  890. num64, isInt = ToInt64(a[argNum])
  891. num = int(num64)
  892. newArgNum = argNum + 1
  893. if tooLarge(num) {
  894. num = 0
  895. isInt = false
  896. }
  897. }
  898. return
  899. }
  900. // parseArgNumber returns the value of the bracketed number, minus 1
  901. // (explicit argument numbers are one-indexed but we want zero-indexed).
  902. // The opening bracket is known to be present at format[0].
  903. // The returned values are the index, the number of bytes to consume
  904. // up to the closing paren, if present, and whether the number parsed
  905. // ok. The bytes to consume will be 1 if no closing paren is present.
  906. func parseArgNumber(format string) (index int, wid int, ok bool) {
  907. // There must be at least 3 bytes: [n].
  908. if len(format) < 3 {
  909. return 0, 1, false
  910. }
  911. // Find closing bracket.
  912. for i := 1; i < len(format); i++ {
  913. if format[i] == ']' {
  914. width, ok, newi := parsenum(format, 1, i)
  915. if !ok || newi != i {
  916. return 0, i + 1, false
  917. }
  918. // arg numbers are one-indexed andskip paren.
  919. return width - 1, i + 1, true
  920. }
  921. }
  922. return 0, 1, false
  923. }
  924. // argNumber returns the next argument to evaluate, which is either the value
  925. // of the passed-in argNum or the value of the bracketed integer that begins
  926. // format[i:]. It also returns the new value of i, that is, the index of the
  927. // next byte of the format to process.
  928. func (p *pp) argNumber(
  929. argNum int,
  930. format string,
  931. i int,
  932. numArgs int,
  933. ) (newArgNum, newi int, found bool) {
  934. if len(format) <= i || format[i] != '[' {
  935. return argNum, i, false
  936. }
  937. p.reordered = true
  938. index, wid, ok := parseArgNumber(format[i:])
  939. if ok && 0 <= index && index < numArgs {
  940. return index, i + wid, true
  941. }
  942. p.goodArgNum = false
  943. return argNum, i + wid, ok
  944. }
  945. func (p *pp) badArgNum(verb rune) {
  946. _, _ = p.WriteString(percentBangString)
  947. _, _ = p.WriteRune(verb)
  948. _, _ = p.WriteString(badIndexString)
  949. }
  950. func (p *pp) missingArg(verb rune) {
  951. _, _ = p.WriteString(percentBangString)
  952. _, _ = p.WriteRune(verb)
  953. _, _ = p.WriteString(missingString)
  954. }
  955. func (p *pp) doFormat(format string, a []Object) (err error) {
  956. defer func() {
  957. if r := recover(); r != nil {
  958. if e, ok := r.(error); ok && e == ErrStringLimit {
  959. err = e
  960. return
  961. }
  962. panic(r)
  963. }
  964. }()
  965. end := len(format)
  966. argNum := 0 // we process one argument per non-trivial format
  967. afterIndex := false // previous item in format was an index like [3].
  968. p.reordered = false
  969. formatLoop:
  970. for i := 0; i < end; {
  971. p.goodArgNum = true
  972. lasti := i
  973. for i < end && format[i] != '%' {
  974. i++
  975. }
  976. if i > lasti {
  977. _, _ = p.WriteString(format[lasti:i])
  978. }
  979. if i >= end {
  980. // done processing format string
  981. break
  982. }
  983. // Process one verb
  984. i++
  985. // Do we have flags?
  986. p.fmt.clearFlags()
  987. simpleFormat:
  988. for ; i < end; i++ {
  989. c := format[i]
  990. switch c {
  991. case '#':
  992. p.fmt.sharp = true
  993. case '0':
  994. // Only allow zero padding to the left.
  995. p.fmt.zero = !p.fmt.minus
  996. case '+':
  997. p.fmt.plus = true
  998. case '-':
  999. p.fmt.minus = true
  1000. p.fmt.zero = false // Do not pad with zeros to the right.
  1001. case ' ':
  1002. p.fmt.space = true
  1003. default:
  1004. // Fast path for common case of ascii lower case simple verbs
  1005. // without precision or width or argument indices.
  1006. if 'a' <= c && c <= 'z' && argNum < len(a) {
  1007. if c == 'v' {
  1008. // Go syntax
  1009. p.fmt.sharpV = p.fmt.sharp
  1010. p.fmt.sharp = false
  1011. // Struct-field syntax
  1012. p.fmt.plusV = p.fmt.plus
  1013. p.fmt.plus = false
  1014. }
  1015. p.printArg(a[argNum], rune(c))
  1016. argNum++
  1017. i++
  1018. continue formatLoop
  1019. }
  1020. // Format is more complex than simple flags and a verb or is
  1021. // malformed.
  1022. break simpleFormat
  1023. }
  1024. }
  1025. // Do we have an explicit argument index?
  1026. argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
  1027. // Do we have width?
  1028. if i < end && format[i] == '*' {
  1029. i++
  1030. p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum)
  1031. if !p.fmt.widPresent {
  1032. _, _ = p.WriteString(badWidthString)
  1033. }
  1034. // We have a negative width, so take its value and ensure
  1035. // that the minus flag is set
  1036. if p.fmt.wid < 0 {
  1037. p.fmt.wid = -p.fmt.wid
  1038. p.fmt.minus = true
  1039. p.fmt.zero = false // Do not pad with zeros to the right.
  1040. }
  1041. afterIndex = false
  1042. } else {
  1043. p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end)
  1044. if afterIndex && p.fmt.widPresent { // "%[3]2d"
  1045. p.goodArgNum = false
  1046. }
  1047. }
  1048. // Do we have precision?
  1049. if i+1 < end && format[i] == '.' {
  1050. i++
  1051. if afterIndex { // "%[3].2d"
  1052. p.goodArgNum = false
  1053. }
  1054. argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
  1055. if i < end && format[i] == '*' {
  1056. i++
  1057. p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum)
  1058. // Negative precision arguments don't make sense
  1059. if p.fmt.prec < 0 {
  1060. p.fmt.prec = 0
  1061. p.fmt.precPresent = false
  1062. }
  1063. if !p.fmt.precPresent {
  1064. _, _ = p.WriteString(badPrecString)
  1065. }
  1066. afterIndex = false
  1067. } else {
  1068. p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i, end)
  1069. if !p.fmt.precPresent {
  1070. p.fmt.prec = 0
  1071. p.fmt.precPresent = true
  1072. }
  1073. }
  1074. }
  1075. if !afterIndex {
  1076. argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a))
  1077. }
  1078. if i >= end {
  1079. _, _ = p.WriteString(noVerbString)
  1080. break
  1081. }
  1082. verb, size := rune(format[i]), 1
  1083. if verb >= utf8.RuneSelf {
  1084. verb, size = utf8.DecodeRuneInString(format[i:])
  1085. }
  1086. i += size
  1087. switch {
  1088. case verb == '%':
  1089. // Percent does not absorb operands and ignores f.wid and f.prec.
  1090. _, _ = p.WriteSingleByte('%')
  1091. case !p.goodArgNum:
  1092. p.badArgNum(verb)
  1093. case argNum >= len(a):
  1094. // No argument left over to print for the current verb.
  1095. p.missingArg(verb)
  1096. case verb == 'v':
  1097. // Go syntax
  1098. p.fmt.sharpV = p.fmt.sharp
  1099. p.fmt.sharp = false
  1100. // Struct-field syntax
  1101. p.fmt.plusV = p.fmt.plus
  1102. p.fmt.plus = false
  1103. fallthrough
  1104. default:
  1105. p.printArg(a[argNum], verb)
  1106. argNum++
  1107. }
  1108. }
  1109. // Check for extra arguments unless the call accessed the arguments
  1110. // out of order, in which case it's too expensive to detect if they've all
  1111. // been used and arguably OK if they're not.
  1112. if !p.reordered && argNum < len(a) {
  1113. p.fmt.clearFlags()
  1114. _, _ = p.WriteString(extraString)
  1115. for i, arg := range a[argNum:] {
  1116. if i > 0 {
  1117. _, _ = p.WriteString(commaSpaceString)
  1118. }
  1119. if arg == nil {
  1120. _, _ = p.WriteString(UndefinedValue.String())
  1121. } else {
  1122. _, _ = p.WriteString(arg.TypeName())
  1123. _, _ = p.WriteSingleByte('=')
  1124. p.printArg(arg, 'v')
  1125. }
  1126. }
  1127. _, _ = p.WriteSingleByte(')')
  1128. }
  1129. return nil
  1130. }
  1131. // Format is like fmt.Sprintf but using Objects.
  1132. func Format(format string, a ...Object) (string, error) {
  1133. p := newPrinter()
  1134. err := p.doFormat(format, a)
  1135. s := string(p.buf)
  1136. p.free()
  1137. return s, err
  1138. }