-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathstring.go
117 lines (102 loc) · 3.39 KB
/
string.go
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
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package string provides the implementation of the python's 'string' module.
package string
import (
"strings"
"github.com/go-python/gpython/py"
)
func init() {
py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "string",
Doc: module_doc,
},
Methods: []*py.Method{
py.MustNewMethod("capwords", capwords, 0, capwords_doc),
},
Globals: py.StringDict{
"whitespace": whitespace,
"ascii_lowercase": ascii_lowercase,
"ascii_uppercase": ascii_uppercase,
"ascii_letters": ascii_letters,
"digits": digits,
"hexdigits": hexdigits,
"octdigits": octdigits,
"punctuation": punctuation,
"printable": printable,
},
})
}
const module_doc = `A collection of string constants.
Public module variables:
whitespace -- a string containing all ASCII whitespace
ascii_lowercase -- a string containing all ASCII lowercase letters
ascii_uppercase -- a string containing all ASCII uppercase letters
ascii_letters -- a string containing all ASCII letters
digits -- a string containing all ASCII decimal digits
hexdigits -- a string containing all ASCII hexadecimal digits
octdigits -- a string containing all ASCII octal digits
punctuation -- a string containing all ASCII punctuation characters
printable -- a string containing all ASCII characters considered printable
`
var (
whitespace = py.String(" \t\n\r\x0b\x0c")
ascii_lowercase = py.String("abcdefghijklmnopqrstuvwxyz")
ascii_uppercase = py.String("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
ascii_letters = ascii_lowercase + ascii_uppercase
digits = py.String("0123456789")
hexdigits = py.String("0123456789abcdefABCDEF")
octdigits = py.String("01234567")
punctuation = py.String("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
printable = py.String("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c")
)
const capwords_doc = `capwords(s [,sep]) -> string
Split the argument into words using split, capitalize each
word using capitalize, and join the capitalized words using
join. If the optional second argument sep is absent or None,
runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise
sep is used to split and join the words.`
func capwords(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
pystr py.Object
pysep py.Object = py.None
)
err := py.ParseTupleAndKeywords(args, kwargs, "s|z", []string{"s", "sep"}, &pystr, &pysep)
if err != nil {
return nil, err
}
pystr = py.String(strings.ToLower(string(pystr.(py.String))))
pyvs, err := pystr.(py.String).Split(py.Tuple{pysep}, nil)
if err != nil {
return nil, err
}
var (
lst = pyvs.(*py.List).Items
vs = make([]string, len(lst))
sep = ""
title = func(s string) string {
if s == "" {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}
)
switch pysep {
case py.None:
for i := range vs {
v := string(lst[i].(py.String))
vs[i] = title(strings.Trim(v, string(whitespace)))
}
sep = " "
default:
sep = string(pysep.(py.String))
for i := range vs {
v := string(lst[i].(py.String))
vs[i] = title(v)
}
}
return py.String(strings.Join(vs, sep)), nil
}