1 | package fmt |
---|---|
2 | |
3 | import ( |
4 | "errors" |
5 | "strings" |
6 | ) |
7 | |
8 | func Sprint(args ...interface{}) string |
9 | |
10 | func Sprintln(args ...interface{}) string { |
11 | return Sprint(args...) + "\n" |
12 | } |
13 | |
14 | func Print(args ...interface{}) (int, error) { |
15 | var n int |
16 | for i, arg := range args { |
17 | if i > 0 { |
18 | print(" ") |
19 | n++ |
20 | } |
21 | msg := Sprint(arg) |
22 | n += len(msg) |
23 | print(msg) |
24 | } |
25 | return n, nil |
26 | } |
27 | |
28 | func Println(args ...interface{}) { |
29 | Print(args...) |
30 | println() |
31 | } |
32 | |
33 | // formatting is too complex to fake |
34 | // handle the bare minimum needed for tests |
35 | |
36 | func Printf(format string, args ...interface{}) (int, error) { |
37 | msg := Sprintf(format, args...) |
38 | print(msg) |
39 | return len(msg), nil |
40 | } |
41 | |
42 | func Sprintf(format string, args ...interface{}) string { |
43 | // handle extremely simple cases that appear in tests. |
44 | if len(format) == 0 { |
45 | return "" |
46 | } |
47 | switch { |
48 | case strings.HasPrefix("%v", format) || strings.HasPrefix("%s", format): |
49 | return Sprint(args[0]) + Sprintf(format[2:], args[1:]...) |
50 | case !strings.HasPrefix("%", format): |
51 | return format[:1] + Sprintf(format[1:], args...) |
52 | default: |
53 | panic("unsupported format string for testing Sprintf") |
54 | } |
55 | } |
56 | |
57 | func Errorf(format string, args ...interface{}) error { |
58 | msg := Sprintf(format, args...) |
59 | return errors.New(msg) |
60 | } |
61 |
Members