-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmt_template.go
More file actions
779 lines (717 loc) · 20 KB
/
Copy pathfmt_template.go
File metadata and controls
779 lines (717 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
package fmt
import "io"
// =============================================================================
// FORMAT TEMPLATE SYSTEM - Printf-style formatting operations
// =============================================================================
// Sprintf formats a string using a printf-style format string and arguments.
// Example: Sprintf("Hello %s", "world") returns "Hello world"
func Sprintf(format string, args ...any) string {
// Inline unifiedFormat logic - eliminated wrapper function
return GetConv().wrFormat(BuffOut, format, args...).String()
}
// Fprintf formats according to a format specifier and writes to w.
// It returns the number of bytes written and any write error encountered.
// Example: Fprintf(os.Stdout, "Hello %s\n", "world")
func Fprintf(w io.Writer, format string, args ...any) (n int, err error) {
// Obtain converter from pool
c := GetConv()
defer c.putConv() // Ensure cleanup
// Use existing wrFormat to populate buffer
c.wrFormat(BuffOut, format, args...)
// Check for formatting errors
if c.hasContent(BuffErr) {
return 0, c
}
// Write to io.Writer
data := c.GetBytes(BuffOut)
return w.Write(data)
}
// Sscanf parses formatted text from a string using printf-style format specifiers.
// It returns the number of items successfully parsed and any error encountered.
// Example: Sscanf("!3F U+003F question", "!%x U+%x %s", &pos, &enc.uv, &enc.name)
func Sscanf(src string, format string, args ...any) (n int, err error) {
// Obtain converter from pool
c := GetConv()
defer c.putConv() // Ensure cleanup
// Reuse parsing logic with format pattern matching
n = c.scanWithFormat(src, format, args...)
// Check for parsing errors
if c.hasContent(BuffErr) {
return n, c
}
return n, nil
}
// applyWidthAndAlignment applies width formatting and alignment to a string
func (c *Conv) applyWidthAndAlignment(str string, width int, leftAlign bool, zeroPad bool) string {
if width <= 0 {
return str
}
strLen := len(str)
pad := width - strLen
if leftAlign {
// Para alineación a la izquierda, agregar padding solo si pad > 0
if pad > 0 {
return str + padString(pad, ' ')
}
return str
} else if pad > 0 {
if zeroPad {
return padString(pad, '0') + str
} else {
return padString(pad, ' ') + str
}
} else if strLen > width {
// Truncar si el string es más largo que el ancho
return str[:width]
}
return str
}
// wrFormat applies printf-style formatting to arguments and writes to specified buffer destination.
// Universal method with dest-first parameter order - follows buffer API architecture
func (c *Conv) wrFormat(dest BuffDest, format string, args ...any) *Conv {
eSz := 0
for _, arg := range args {
switch arg.(type) {
case int, int8, int16, int32, int64:
eSz += 16 // Estimate for integers
case uint, uint8, uint16, uint32, uint64:
eSz += 16 // Estimate for unsigned integers
case float64, float32:
eSz += 24 // Estimate for floats
default:
eSz += 16 // Default estimate
}
}
// Reset buffer at start BEFORE capacity estimation to avoid contamination
c.ResetBuffer(dest)
argIndex := 0
for i := 0; i < len(format); i++ {
if format[i] == '%' {
i++
// Parse format specifier using shared helper
formatChar, param, formatSpec, width, leftAlign, zeroPad, newI := c.parseFormatSpecifier(format, i)
i = newI
// Handle literal %
if formatChar == '%' {
c.wrByte(dest, '%')
continue
}
// Validate format specifier using shared validation
if !c.isValidWriteFormatChar(formatChar) {
c.wrErr("format", "provided", "not", "supported", byte(formatChar))
return c
}
if argIndex >= len(args) {
c.wrErr("argument", "missing", formatSpec)
return c
}
// Format value using shared helper
arg := args[argIndex]
// Only a GROWTH of the error buffer means formatValue itself failed. Testing
// hasContent(BuffErr) instead would be a false positive whenever dest IS
// BuffErr (Errf): the literal text already written would look like a failure,
// and the message would be silently truncated at the first verb.
errLenBefore := c.errLen
str := c.formatValue(arg, formatChar, param, formatSpec)
if c.errLen > errLenBefore {
return c
}
// Apply width and alignment if needed
str = c.applyWidthAndAlignment(str, width, leftAlign, zeroPad)
argIndex++
c.wrBytes(dest, []byte(str))
continue
} else {
c.wrByte(dest, format[i])
}
}
if !c.hasContent(BuffErr) {
// Final output is ready in dest buffer
c.kind = K.String
}
return c
}
// parseFormatSpecifier extracts format specifier and parameters from format string
// Returns formatChar, param, formatSpec, width, leftAlign, zeroPad, and new index position
func (c *Conv) parseFormatSpecifier(format string, i int) (formatChar rune, param int, formatSpec string, width int, leftAlign bool, zeroPad bool, newI int) {
// Parse flags
for i < len(format) {
if format[i] == '-' {
leftAlign = true
i++
} else if format[i] == '0' {
zeroPad = true
i++
} else {
break
}
}
// Parse width
w := 0
for i < len(format) && format[i] >= '0' && format[i] <= '9' {
w = w*10 + int(format[i]-'0')
i++
}
if w > 0 {
width = w
}
// Parse precision for floats
precision := -1
if i < len(format) && format[i] == '.' {
i++
p := 0
for i < len(format) && format[i] >= '0' && format[i] <= '9' {
p = p*10 + int(format[i]-'0')
i++
}
precision = p
}
if i >= len(format) {
return 0, 0, "", 0, false, false, i
}
// Parse format character and return parameters
switch format[i] {
case 'c':
formatChar, param, formatSpec = 'c', 0, "%c"
case 'U':
formatChar, param, formatSpec = 'U', 0, "%U"
case 'd':
formatChar, param, formatSpec = 'd', 10, "%d"
case 'u':
formatChar, param, formatSpec = 'u', 10, "%u"
case 'f':
formatChar, param, formatSpec = 'f', precision, "%f"
case 'e':
formatChar, param, formatSpec = 'e', precision, "%e"
case 'E':
formatChar, param, formatSpec = 'E', precision, "%E"
case 'g':
formatChar, param, formatSpec = 'g', precision, "%g"
case 'G':
formatChar, param, formatSpec = 'G', precision, "%G"
case 'o':
formatChar, param, formatSpec = 'o', 8, "%o"
case 'O':
formatChar, param, formatSpec = 'O', 8, "%O"
case 'b':
formatChar, param, formatSpec = 'b', 2, "%b"
case 'B':
formatChar, param, formatSpec = 'B', 2, "%B"
case 'x':
formatChar, param, formatSpec = 'x', 16, "%x"
case 'X':
formatChar, param, formatSpec = 'X', 16, "%X"
case 'p':
formatChar, param, formatSpec = 'p', 0, "%p"
case 't':
formatChar, param, formatSpec = 't', 0, "%t"
case 'v':
formatChar, param, formatSpec = 'v', 0, "%v"
case 'q':
formatChar, param, formatSpec = 'q', 0, "%q"
case 's':
formatChar, param, formatSpec = 's', 0, "%s"
case '%':
formatChar, param, formatSpec = '%', 0, "%%"
case 'L':
formatChar, param, formatSpec = 'L', 0, "%L"
case 'w':
formatChar, param, formatSpec = 'w', 0, "%w"
default:
formatChar, param, formatSpec = rune(format[i]), 0, ""
}
return formatChar, param, formatSpec, width, leftAlign, zeroPad, i
}
// isValidFormatChar validates format characters for both read and write operations
func (c *Conv) isValidFormatChar(ch rune) bool {
switch ch {
case 'c', 'U', 'd', 'u', 'f', 'e', 'E', 'g', 'G', 'o', 'O', 'b', 'B', 'x', 'X', 'p', 't', 'v', 'q', 's', '%', 'L', 'w':
return true
default:
return false
}
}
// isValidWriteFormatChar validates format characters for write operations (reuses isValidFormatChar)
func (c *Conv) isValidWriteFormatChar(ch rune) bool {
return c.isValidFormatChar(ch)
}
// spaces returns a string with n spaces
func spaces(n int) string {
if n <= 0 {
return ""
}
b := make([]byte, n)
for i := range b {
b[i] = ' '
}
return string(b)
}
// padString returns a string with n characters of the specified byte
func padString(n int, ch byte) string {
if n <= 0 {
return ""
}
b := make([]byte, n)
for i := range b {
b[i] = ch
}
return string(b)
}
// wrInvalidTypeErr writes an invalid type error for the given format spec
func (c *Conv) wrInvalidTypeErr(formatSpec string) {
c.wrErr("invalid", "type", "of", "argument", formatSpec)
}
// formatValue formats a single value according to format character
func (c *Conv) formatValue(arg any, formatChar rune, param int, formatSpec string) string {
switch formatChar {
case 'c':
// Character formatting: accept rune, byte, int
var ch rune
var ok bool
switch v := arg.(type) {
case rune:
ch = v
ok = true
case byte:
ch = rune(v)
ok = true
case int:
ch = rune(v)
ok = true
}
if ok {
return string(ch)
} else {
c.wrInvalidTypeErr("%c")
return ""
}
case 'U':
// Unicode code point formatting: U+XXXX (always uppercase hex, at least 4 digits)
var r rune
var ok bool
switch v := arg.(type) {
case rune:
r = v
ok = true
case int:
r = rune(v)
ok = true
}
if ok {
code := int(r)
c.ResetBuffer(BuffWork)
c.WrIntBase(BuffWork, int64(code), 16, false, true)
// Pad to at least 4 digits by checking buffer length directly
for c.workLen < 4 {
// Prepend '0' by shifting existing content
if c.workLen+1 > len(c.work) {
c.work = append(c.work, 0) // Expand capacity if needed
}
// Shift existing content right
copy(c.work[1:c.workLen+1], c.work[:c.workLen])
c.work[0] = '0'
c.workLen++
}
// Build "U+" prefix + hex directly in output
return "U+" + c.GetString(BuffWork) // Only allocation when needed
} else {
c.wrInvalidTypeErr("%U")
return ""
}
case 'p':
// Pointer formatting: always print '0x' for any pointer value
return "0x"
case 'g', 'G':
// Compact float formatting (manual, no stdlib)
if floatVal, ok := c.toFloat64(arg); ok {
c.ResetBuffer(BuffWork)
compact := formatCompactFloat(floatVal, param, formatChar == 'G')
c.WrString(BuffWork, compact)
return c.GetString(BuffWork) // Keep for compatibility with formatFloat usage
} else {
c.wrInvalidTypeErr(formatSpec)
return ""
}
case 'e', 'E':
// Scientific notation (manual, no stdlib)
if floatVal, ok := c.toFloat64(arg); ok {
c.ResetBuffer(BuffWork)
sci := formatScientific(floatVal, param, formatChar == 'E')
c.WrString(BuffWork, sci)
return c.GetString(BuffWork)
} else {
c.wrInvalidTypeErr(formatSpec)
return ""
}
case 'q':
// Quoted string or rune
switch v := arg.(type) {
case string:
return Convert(v).Quote().String()
case rune:
if v == '\'' {
return "'\\''"
}
s := Convert(string(v)).Quote().String()
if len(s) >= 2 {
return "'" + s[1:len(s)-1] + "'"
}
return "'" + string(v) + "'"
case byte:
if v == '\'' {
return "'\\''"
}
s := Convert(string(rune(v))).Quote().String()
if len(s) >= 2 {
return "'" + s[1:len(s)-1] + "'"
}
return "'" + string(rune(v)) + "'"
}
c.wrInvalidTypeErr(formatSpec)
return ""
case 't':
// Boolean formatting
if bval, ok := arg.(bool); ok {
if bval {
return "true"
} else {
return "false"
}
} else {
c.wrInvalidTypeErr(formatSpec)
return ""
}
case 'd', 'o', 'b', 'x', 'O', 'B', 'X':
if intVal, ok := c.toInt64(arg); ok {
c.ResetBuffer(BuffWork)
// Use uppercase for 'X', 'O', 'B'
upper := formatChar == 'X' || formatChar == 'O' || formatChar == 'B'
if param == 10 {
c.WrIntBase(BuffWork, intVal, 10, true, upper)
} else {
c.WrIntBase(BuffWork, intVal, param, true, upper)
}
return c.GetString(BuffWork)
} else {
c.wrInvalidTypeErr(formatSpec)
return ""
}
case 'u':
if uintVal, ok := c.toUint64(arg); ok {
c.ResetBuffer(BuffWork)
c.wrUintBase(BuffWork, uintVal, 10)
return c.GetString(BuffWork)
} else {
c.wrInvalidTypeErr(formatSpec)
return ""
}
case 'f':
if floatVal, ok := c.toFloat64(arg); ok {
c.ResetBuffer(BuffWork)
if param >= 0 {
c.wrFloatWithPrecision(BuffWork, floatVal, param)
} else {
c.WrFloat64(BuffWork, floatVal)
}
return c.GetString(BuffWork)
} else {
c.wrInvalidTypeErr(formatSpec)
return ""
}
case 's':
// String formatting - handle both string and types with String() method
if strVal, ok := arg.(string); ok {
return strVal
}
// Handle custom types with String() method using AnyToBuff
c.ResetBuffer(BuffWork)
c.AnyToBuff(BuffWork, arg)
if c.hasContent(BuffErr) {
// If AnyToBuff fails, reset error and return empty with proper error
c.wrInvalidTypeErr(formatSpec)
return ""
}
return c.GetString(BuffWork)
case 'v':
c.ResetBuffer(BuffWork)
if errVal, ok := arg.(error); ok {
c.WrString(BuffWork, errVal.Error())
return c.GetString(BuffWork)
} else {
c.AnyToBuff(BuffWork, arg)
if c.hasContent(BuffErr) {
return ""
}
return c.GetString(BuffWork)
}
case 'L':
// Localized string formatting using hook
if strVal, ok := arg.(string); ok {
return tr(strVal)
}
c.wrInvalidTypeErr(formatSpec)
return ""
case 'w':
// Error wrapping - treat as %v (error or custom String())
c.ResetBuffer(BuffWork)
if errVal, ok := arg.(error); ok {
c.WrString(BuffWork, errVal.Error())
return c.GetString(BuffWork)
} else {
c.AnyToBuff(BuffWork, arg)
if c.hasContent(BuffErr) {
return ""
}
return c.GetString(BuffWork)
}
}
return ""
}
// scanWithFormat parses formatted text from a string, reusing wrFormat logic
// Returns the number of items successfully parsed
func (c *Conv) scanWithFormat(src string, format string, args ...any) int {
srcPos := 0
fmtPos := 0
parsed := 0
for fmtPos < len(format) && srcPos <= len(src) {
if format[fmtPos] == '%' {
fmtPos++
if fmtPos >= len(format) {
break
}
// Parse format specifier using same logic as wrFormat
formatChar := rune(format[fmtPos])
// Handle percent literal (%%)
if formatChar == '%' {
// This is a literal % character - match it in source
if srcPos >= len(src) || src[srcPos] != '%' {
c.wrErr("format", "invalid", "literal mismatch")
return parsed
}
srcPos++
fmtPos++
continue
}
// Validate format specifier (reuse wrFormat validation)
if !c.isValidFormatChar(formatChar) {
c.wrErr("format", "not", "supported", format[fmtPos])
return parsed
}
if parsed >= len(args) {
c.wrErr("argument", "missing")
return parsed
}
// Extract and parse value from source
valueStr, newPos := c.extractValue(src, srcPos, formatChar)
if valueStr == "" {
return parsed
}
// Convert and assign using existing conversion logic
if c.assignParsedValue(valueStr, formatChar, args[parsed]) {
parsed++
} else {
// For type validation errors, preserve the error
// For parsing failures (empty valueStr from non-parseable input), clear error
if valueStr != "" {
// Non-empty valueStr suggests a type validation error, preserve it
return parsed
} else {
// Empty valueStr suggests parsing failure, clear error for partial parsing
c.ResetBuffer(BuffErr)
return parsed
}
}
srcPos = newPos
fmtPos++
} else {
// Literal character - must match (reuse wrFormat literal logic)
if srcPos >= len(src) || src[srcPos] != format[fmtPos] {
c.wrErr("format", "invalid", "literal mismatch")
return parsed
}
srcPos++
fmtPos++
}
}
return parsed
}
// parseNumber extracts a number from string starting at pos
func (c *Conv) parseNumber(src string, pos int, allowSign bool) int {
if allowSign && pos < len(src) && (src[pos] == '-' || src[pos] == '+') {
pos++
}
for pos < len(src) && src[pos] >= '0' && src[pos] <= '9' {
pos++
}
return pos
}
// parseHexNumber extracts a hexadecimal number from string starting at pos
func (c *Conv) parseHexNumber(src string, pos int) int {
for pos < len(src) && ((src[pos] >= '0' && src[pos] <= '9') ||
(src[pos] >= 'a' && src[pos] <= 'f') ||
(src[pos] >= 'A' && src[pos] <= 'F')) {
pos++
}
return pos
}
// extractValue extracts a value from source string based on format character
func (c *Conv) extractValue(src string, pos int, formatChar rune) (string, int) {
start := pos
switch formatChar {
case 'd':
// Extract decimal number (reuse number parsing logic)
pos = c.parseNumber(src, pos, true)
case 'x', 'X':
// Extract hexadecimal number
pos = c.parseHexNumber(src, pos)
case 'f', 'g', 'e':
// Extract floating point number (reuse float parsing logic)
pos = c.parseNumber(src, pos, true)
if pos < len(src) && src[pos] == '.' {
pos++
pos = c.parseNumber(src, pos, false)
}
case 's':
// Extract string until whitespace
for pos < len(src) && src[pos] != ' ' && src[pos] != '\t' &&
src[pos] != '\n' && src[pos] != '\r' {
pos++
}
case 'c':
// Extract single character
if pos < len(src) {
pos++
}
case '%':
// Literal %
if pos < len(src) && src[pos] == '%' {
pos++
return "%", pos
}
c.wrErr("format", "invalid", "expected %")
return "", pos
}
if start == pos {
// No characters extracted - this is not an error for partial parsing
return "", pos
}
return src[start:pos], pos
}
// assignParsedValue converts and assigns a parsed value using existing conversion logic
func (c *Conv) assignParsedValue(valueStr string, formatChar rune, arg any) bool {
switch formatChar {
case 'd':
// Use buffer-based integer conversion instead of creating new Conv
c.ResetBuffer(BuffWork)
c.WrString(BuffWork, valueStr)
c.swapBuff(BuffOut, BuffErr) // Save current BuffOut
c.swapBuff(BuffWork, BuffOut) // Move valueStr to BuffOut
switch ptr := arg.(type) {
case *int:
if val, err := c.Int(); err == nil {
*ptr = val
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
return true
}
case *int64:
if val, err := c.Int64(); err == nil {
*ptr = val
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
return true
}
case *int32:
if val, err := c.Int32(); err == nil {
*ptr = val
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
return true
}
}
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
case 'x', 'X':
// Reuse hexadecimal conversion logic from wrFormat
val := c.parseHexString(valueStr)
switch ptr := arg.(type) {
case *int:
*ptr = int(val)
return true
case *int64:
*ptr = val
return true
case *int32:
*ptr = int32(val)
return true
case *uint:
*ptr = uint(val)
return true
case *uint32:
*ptr = uint32(val)
return true
case *uint64:
*ptr = uint64(val)
return true
}
case 'f', 'g', 'e':
// Use buffer-based float conversion instead of creating new Conv
c.ResetBuffer(BuffWork)
c.WrString(BuffWork, valueStr)
c.swapBuff(BuffOut, BuffErr) // Save current BuffOut
c.swapBuff(BuffWork, BuffOut) // Move valueStr to BuffOut
switch ptr := arg.(type) {
case *float64:
if val, err := c.Float64(); err == nil {
*ptr = val
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
return true
}
case *float32:
if val, err := c.Float32(); err == nil {
*ptr = val
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
return true
}
}
c.swapBuff(BuffOut, BuffWork) // Clear BuffOut
c.swapBuff(BuffErr, BuffOut) // Restore original BuffOut
case 's':
// Direct string assignment
if ptr, ok := arg.(*string); ok {
*ptr = valueStr
return true
}
case 'c':
// Character assignment
if len(valueStr) > 0 {
switch ptr := arg.(type) {
case *rune:
*ptr = rune(valueStr[0])
return true
case *byte:
*ptr = valueStr[0]
return true
}
}
}
c.wrErr("invalid", "type", "of", "argument")
return false
}
// parseHexString converts hex string to int64 (extracted and optimized from parseScanf)
func (c *Conv) parseHexString(hexStr string) int64 {
val := int64(0)
for _, ch := range hexStr {
val *= 16
if ch >= '0' && ch <= '9' {
val += int64(ch - '0')
} else if ch >= 'a' && ch <= 'f' {
val += int64(ch - 'a' + 10)
} else if ch >= 'A' && ch <= 'F' {
val += int64(ch - 'A' + 10)
}
}
return val
}