OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package fmt | 5 package fmt |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "io" | 9 "io" |
10 "os" | 10 "os" |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 381 } |
382 | 382 |
383 // typeError indicates that the type of the operand did not match the format | 383 // typeError indicates that the type of the operand did not match the format |
384 func (s *ss) typeError(field interface{}, expected string) { | 384 func (s *ss) typeError(field interface{}, expected string) { |
385 s.errorString("expected field of type pointer to " + expected + "; found
" + reflect.Typeof(field).String()) | 385 s.errorString("expected field of type pointer to " + expected + "; found
" + reflect.Typeof(field).String()) |
386 } | 386 } |
387 | 387 |
388 var complexError = os.ErrorString("syntax error scanning complex number") | 388 var complexError = os.ErrorString("syntax error scanning complex number") |
389 var boolError = os.ErrorString("syntax error scanning boolean") | 389 var boolError = os.ErrorString("syntax error scanning boolean") |
390 | 390 |
391 // accepts checks the next rune in the input. If it's a byte (sic) in the strin
g, it puts it in the | 391 // consume reads the next rune in the input and reports whether it is in the ok
string. |
392 // buffer and returns true. Otherwise it return false. | 392 // If accept is true, it puts the character into the input token. |
393 func (s *ss) accept(ok string) bool { | 393 func (s *ss) consume(ok string, accept bool) bool { |
394 if s.wid >= s.maxWid { | 394 if s.wid >= s.maxWid { |
395 return false | 395 return false |
396 } | 396 } |
397 rune := s.getRune() | 397 rune := s.getRune() |
398 if rune == EOF { | 398 if rune == EOF { |
399 return false | 399 return false |
400 } | 400 } |
401 for i := 0; i < len(ok); i++ { | 401 for i := 0; i < len(ok); i++ { |
402 if int(ok[i]) == rune { | 402 if int(ok[i]) == rune { |
403 » » » s.buf.WriteRune(rune) | 403 » » » if accept { |
404 » » » s.wid++ | 404 » » » » s.buf.WriteRune(rune) |
| 405 » » » » s.wid++ |
| 406 » » » } |
405 return true | 407 return true |
406 } | 408 } |
407 } | 409 } |
408 » if rune != EOF { | 410 » if rune != EOF && accept { |
409 s.UngetRune() | 411 s.UngetRune() |
410 } | 412 } |
411 return false | 413 return false |
412 } | 414 } |
413 | 415 |
| 416 // accept checks the next rune in the input. If it's a byte (sic) in the string
, it puts it in the |
| 417 // buffer and returns true. Otherwise it return false. |
| 418 func (s *ss) accept(ok string) bool { |
| 419 return s.consume(ok, true) |
| 420 } |
| 421 |
414 // okVerb verifies that the verb is present in the list, setting s.err appropria
tely if not. | 422 // okVerb verifies that the verb is present in the list, setting s.err appropria
tely if not. |
415 func (s *ss) okVerb(verb int, okVerbs, typ string) bool { | 423 func (s *ss) okVerb(verb int, okVerbs, typ string) bool { |
416 for _, v := range okVerbs { | 424 for _, v := range okVerbs { |
417 if v == verb { | 425 if v == verb { |
418 return true | 426 return true |
419 } | 427 } |
420 } | 428 } |
421 s.errorString("bad verb %" + string(verb) + " for " + typ) | 429 s.errorString("bad verb %" + string(verb) + " for " + typ) |
422 return false | 430 return false |
423 } | 431 } |
(...skipping 29 matching lines...) Expand all Loading... |
453 octalDigits = "01234567" | 461 octalDigits = "01234567" |
454 decimalDigits = "0123456789" | 462 decimalDigits = "0123456789" |
455 hexadecimalDigits = "0123456789aAbBcCdDeEfF" | 463 hexadecimalDigits = "0123456789aAbBcCdDeEfF" |
456 sign = "+-" | 464 sign = "+-" |
457 period = "." | 465 period = "." |
458 exponent = "eE" | 466 exponent = "eE" |
459 ) | 467 ) |
460 | 468 |
461 // getBase returns the numeric base represented by the verb and its digit string
. | 469 // getBase returns the numeric base represented by the verb and its digit string
. |
462 func (s *ss) getBase(verb int) (base int, digits string) { | 470 func (s *ss) getBase(verb int) (base int, digits string) { |
463 » s.okVerb(verb, "bdoxXv", "integer") // sets s.err | 471 » s.okVerb(verb, "bdoUxXv", "integer") // sets s.err |
464 base = 10 | 472 base = 10 |
465 digits = decimalDigits | 473 digits = decimalDigits |
466 switch verb { | 474 switch verb { |
467 case 'b': | 475 case 'b': |
468 base = 2 | 476 base = 2 |
469 digits = binaryDigits | 477 digits = binaryDigits |
470 case 'o': | 478 case 'o': |
471 base = 8 | 479 base = 8 |
472 digits = octalDigits | 480 digits = octalDigits |
473 » case 'x', 'X': | 481 » case 'x', 'X', 'U': |
474 base = 16 | 482 base = 16 |
475 digits = hexadecimalDigits | 483 digits = hexadecimalDigits |
476 } | 484 } |
477 return | 485 return |
478 } | 486 } |
479 | 487 |
480 // scanNumber returns the numerical string with specified digits starting here. | 488 // scanNumber returns the numerical string with specified digits starting here. |
481 func (s *ss) scanNumber(digits string) string { | 489 func (s *ss) scanNumber(digits string) string { |
482 if !s.accept(digits) { | 490 if !s.accept(digits) { |
483 s.errorString("expected integer") | 491 s.errorString("expected integer") |
(...skipping 15 matching lines...) Expand all Loading... |
499 } | 507 } |
500 | 508 |
501 // scanInt returns the value of the integer represented by the next | 509 // scanInt returns the value of the integer represented by the next |
502 // token, checking for overflow. Any error is stored in s.err. | 510 // token, checking for overflow. Any error is stored in s.err. |
503 func (s *ss) scanInt(verb int, bitSize int) int64 { | 511 func (s *ss) scanInt(verb int, bitSize int) int64 { |
504 if verb == 'c' { | 512 if verb == 'c' { |
505 return s.scanRune(bitSize) | 513 return s.scanRune(bitSize) |
506 } | 514 } |
507 base, digits := s.getBase(verb) | 515 base, digits := s.getBase(verb) |
508 s.skipSpace(false) | 516 s.skipSpace(false) |
509 » s.accept(sign) // If there's a sign, it will be left in the token buffer
. | 517 » if verb == 'U' { |
| 518 » » if !s.consume("U", false) || !s.consume("+", false) { |
| 519 » » » s.errorString("bad unicode format ") |
| 520 » » } |
| 521 » } else { |
| 522 » » s.accept(sign) // If there's a sign, it will be left in the toke
n buffer. |
| 523 » } |
510 tok := s.scanNumber(digits) | 524 tok := s.scanNumber(digits) |
511 i, err := strconv.Btoi64(tok, base) | 525 i, err := strconv.Btoi64(tok, base) |
512 if err != nil { | 526 if err != nil { |
513 s.error(err) | 527 s.error(err) |
514 } | 528 } |
515 n := uint(bitSize) | 529 n := uint(bitSize) |
516 x := (i << (64 - n)) >> (64 - n) | 530 x := (i << (64 - n)) >> (64 - n) |
517 if x != i { | 531 if x != i { |
518 s.errorString("integer overflow on token " + tok) | 532 s.errorString("integer overflow on token " + tok) |
519 } | 533 } |
520 return i | 534 return i |
521 } | 535 } |
522 | 536 |
523 // scanUint returns the value of the unsigned integer represented | 537 // scanUint returns the value of the unsigned integer represented |
524 // by the next token, checking for overflow. Any error is stored in s.err. | 538 // by the next token, checking for overflow. Any error is stored in s.err. |
525 func (s *ss) scanUint(verb int, bitSize int) uint64 { | 539 func (s *ss) scanUint(verb int, bitSize int) uint64 { |
526 if verb == 'c' { | 540 if verb == 'c' { |
527 return uint64(s.scanRune(bitSize)) | 541 return uint64(s.scanRune(bitSize)) |
528 } | 542 } |
529 base, digits := s.getBase(verb) | 543 base, digits := s.getBase(verb) |
530 s.skipSpace(false) | 544 s.skipSpace(false) |
| 545 if verb == 'U' { |
| 546 if !s.consume("U", false) || !s.consume("+", false) { |
| 547 s.errorString("bad unicode format ") |
| 548 } |
| 549 } |
531 tok := s.scanNumber(digits) | 550 tok := s.scanNumber(digits) |
532 i, err := strconv.Btoui64(tok, base) | 551 i, err := strconv.Btoui64(tok, base) |
533 if err != nil { | 552 if err != nil { |
534 s.error(err) | 553 s.error(err) |
535 } | 554 } |
536 n := uint(bitSize) | 555 n := uint(bitSize) |
537 x := (i << (64 - n)) >> (64 - n) | 556 x := (i << (64 - n)) >> (64 - n) |
538 if x != i { | 557 if x != i { |
539 s.errorString("unsigned integer overflow on token " + tok) | 558 s.errorString("unsigned integer overflow on token " + tok) |
540 } | 559 } |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 field := a[numProcessed] | 983 field := a[numProcessed] |
965 | 984 |
966 s.scanOne(c, field) | 985 s.scanOne(c, field) |
967 numProcessed++ | 986 numProcessed++ |
968 } | 987 } |
969 if numProcessed < len(a) { | 988 if numProcessed < len(a) { |
970 s.errorString("too many operands") | 989 s.errorString("too many operands") |
971 } | 990 } |
972 return | 991 return |
973 } | 992 } |
OLD | NEW |