1
1
SUPERSTRING_MINIMAL_LENGTH = 48
2
2
3
+
3
4
class SuperStringBase (object ):
4
5
def length (self ):
5
6
pass
6
7
7
8
def lower (self ):
8
9
if self .length () < SUPERSTRING_MINIMAL_LENGTH :
9
- return SuperString (self .toPrintable ().lower ())
10
+ return SuperString (self .to_printable ().lower ())
10
11
return SuperStringLower (self )
11
12
12
13
def upper (self ):
13
14
if self .length () < SUPERSTRING_MINIMAL_LENGTH :
14
- return SuperString (self .toPrintable ().upper ())
15
+ return SuperString (self .to_printable ().upper ())
15
16
return SuperStringUpper (self )
16
17
17
- def characterAt (self , index ):
18
+ def character_at (self , index ):
18
19
pass
19
20
20
- def split (self , separator = " " ):
21
+ def split (self , separator = " " ):
21
22
result = []
22
23
previous = 0
23
24
i = 0
24
25
while i < self .length ():
25
- gonext = False
26
- j = 0
27
- while j < len (separator ):
28
- if self .characterAt (i + j ) != separator [j ]:
29
- gonext = True
26
+ for j in range (len (separator )):
27
+ if self .character_at (i + j ) != separator [j ]:
30
28
break
31
- j = j + 1
32
- if not gonext :
29
+ else :
33
30
result .append (self .substring (previous , i ))
34
31
previous = i + len (separator )
35
32
i = i + 1
36
33
result .append (self .substring (previous ))
37
34
return result
38
35
39
- def substring (self , startIndex , endIndex = None ):
40
- # TODO: if the substring is to short: copy
41
- if startIndex == None :
42
- startIndex = 0
43
- if endIndex == None :
44
- endIndex = self .length ()
45
- if startIndex == 0 and endIndex == self .length ():
36
+ def substring (self , start_index , end_index = None ):
37
+ # TODO: if the substring is to short: copys
38
+ start_index = start_index if start_index is not None else 0
39
+ end_index = end_index if end_index is not None else self .length ()
40
+ if start_index == 0 and end_index == self .length ():
46
41
return self
47
- if endIndex - startIndex < SUPERSTRING_MINIMAL_LENGTH :
48
- return SuperString (self .toPrintable ( startIndex , endIndex = endIndex ))
49
- return SuperStringSubstring (self , startIndex , endIndex )
42
+ if end_index - start_index < SUPERSTRING_MINIMAL_LENGTH :
43
+ return SuperString (self .to_printable ( start_index , end_index = end_index ))
44
+ return SuperStringSubstring (self , start_index , end_index )
50
45
51
46
def strip (self ):
52
47
i = 0
53
- while self .characterAt (i ) == ' ' :
48
+ while self .character_at (i ) == ' ' :
54
49
i = i + 1
55
- startIndex = i
50
+ start_index = i
56
51
i = self .length () - 1
57
- while self .characterAt (i ) == ' ' :
52
+ while self .character_at (i ) == ' ' :
58
53
i = i - 1
59
- return self .substring (startIndex , i + 1 )
54
+ return self .substring (start_index , i + 1 )
60
55
61
- def toPrintable (self , startIndex = None , endIndex = None ):
56
+ def to_printable (self , start_index = None , end_index = None ):
62
57
pass
63
58
64
59
def __len__ (self ):
@@ -70,35 +65,33 @@ def __add__(self, right):
70
65
return SuperStringConcatenation (self , right )
71
66
72
67
def __str__ (self ):
73
- return self .toPrintable ()
68
+ return self .to_printable ()
74
69
75
70
def __getitem__ (self , key ):
76
71
if isinstance (key , slice ):
77
72
start = key .start if key .start > 0 else self .length () + key .start
78
73
stop = key .stop if key .stop > 0 else self .length () + key .stop
79
74
return self .substring (start , end_index = stop )
80
- return self .characterAt (key )
75
+ return self .character_at (key )
76
+
81
77
82
78
class SuperString (SuperStringBase ):
83
79
def __init__ (self , content ):
84
80
self ._content = content
85
81
86
82
def length (self ):
87
- if not hasattr (self ,'_length' ):
83
+ if not hasattr (self , '_length' ):
88
84
self ._length = len (self ._content )
89
85
return self ._length
90
86
91
- def characterAt (self , index ):
87
+ def character_at (self , index ):
92
88
return self ._content [index ]
93
89
94
- def toPrintable (self , startIndex = None , endIndex = None ):
95
- if startIndex != None :
96
- if endIndex == None :
97
- endIndex = self .length ()
98
- return self ._content [startIndex :endIndex ]
99
- elif endIndex != None :
100
- return self ._content [0 :endIndex ]
101
- return self ._content
90
+ def to_printable (self , start_index = None , end_index = None ):
91
+ start_index = start_index if start_index is not None else 0
92
+ end_index = end_index if end_index is not None else self .length ()
93
+ return self ._content [start_index :end_index ]
94
+
102
95
103
96
class SuperStringConcatenation (SuperStringBase ):
104
97
def __init__ (self , left , right ):
@@ -108,44 +101,43 @@ def __init__(self, left, right):
108
101
def length (self ):
109
102
return self ._left .length () + self ._right .length ()
110
103
111
- def characterAt (self , index ):
112
- leftLen = self ._left .length ()
113
- if index < leftLen :
104
+ def character_at (self , index ):
105
+ left_len = self ._left .length ()
106
+ if index < left_len :
114
107
return self ._left [index ]
115
- return self ._right [index - leftLen ]
116
-
117
- def toPrintable (self , startIndex = None , endIndex = None ):
118
- if startIndex == None :
119
- startIndex = 0
120
- if endIndex == None :
121
- endIndex = self . length ()
122
- leftLen = self ._left .length ( )
123
- if endIndex < leftLen :
124
- return self ._left . toPrintable ( startIndex = startIndex , endIndex = endIndex )
125
- elif startIndex > leftLen :
126
- return self . _right . toPrintable ( startIndex = startIndex - leftLen , endIndex = endIndex - leftLen )
127
- return self . _left . toPrintable ( startIndex = startIndex ) + self . _right . toPrintable ( endIndex = endIndex - leftLen )
108
+ return self ._right [index - left_len ]
109
+
110
+ def to_printable (self , start_index = None , end_index = None ):
111
+ start_index = start_index if start_index is not None else 0
112
+ end_index = end_index if end_index is not None else self . length ()
113
+ left_len = self . _left . length ()
114
+ if end_index < left_len :
115
+ return self ._left .to_printable ( start_index = start_index , end_index = end_index )
116
+ elif start_index > left_len :
117
+ return self ._right . to_printable ( start_index = start_index - left_len , end_index = end_index - left_len )
118
+ return self . _left . to_printable ( start_index = start_index ) + self . _right . to_printable (
119
+ end_index = end_index - left_len )
120
+
128
121
129
122
class SuperStringSubstring (SuperStringBase ):
130
- def __init__ (self , base , startIndex , endIndex ):
123
+ def __init__ (self , base , start_index , end_index ):
131
124
self ._base = base
132
- self ._startIndex = startIndex
133
- self ._endIndex = endIndex
125
+ self ._start_index = start_index
126
+ self ._end_index = end_index
134
127
135
128
def length (self ):
136
- return self ._endIndex - self ._startIndex
129
+ return self ._end_index - self ._start_index
130
+
131
+ def character_at (self , index ):
132
+ return self ._base .character_at (self ._start_index + index )
137
133
138
- def characterAt (self , index ):
139
- return self ._base .characterAt (self ._startIndex + index )
134
+ def to_printable (self , start_index = None , end_index = None ):
135
+ start_index = start_index if start_index is not None else 0
136
+ end_index = end_index if end_index is not None else self .length ()
137
+ start_index += self ._start_index
138
+ end_index += self ._start_index
139
+ return self ._base .to_printable (start_index , end_index = end_index )
140
140
141
- def toPrintable (self , startIndex = None , endIndex = None ):
142
- if startIndex == None :
143
- startIndex = 0
144
- if endIndex == None :
145
- endIndex = self .length ()
146
- startIndex += self ._startIndex
147
- endIndex += self ._startIndex
148
- return self ._base .toPrintable (startIndex , endIndex = endIndex )
149
141
150
142
class SuperStringLower (SuperStringBase ):
151
143
def __init__ (self , base ):
@@ -154,11 +146,12 @@ def __init__(self, base):
154
146
def length (self ):
155
147
return self ._base .length ()
156
148
157
- def characterAt (self , index ):
158
- return self ._base .characterAt (index ).lower ()
149
+ def character_at (self , index ):
150
+ return self ._base .character_at (index ).lower ()
151
+
152
+ def to_printable (self , start_index = None , end_index = None ):
153
+ return self ._base .to_printable (start_index , end_index ).lower ()
159
154
160
- def toPrintable (self , startIndex = None , endIndex = None ):
161
- return self ._base .toPrintable (startIndex , endIndex ).lower ()
162
155
163
156
class SuperStringUpper (SuperStringBase ):
164
157
def __init__ (self , base ):
@@ -167,8 +160,8 @@ def __init__(self, base):
167
160
def length (self ):
168
161
return self ._base .length ()
169
162
170
- def characterAt (self , index ):
171
- return self ._base .characterAt (index ).upper ()
163
+ def character_at (self , index ):
164
+ return self ._base .character_at (index ).upper ()
172
165
173
- def toPrintable (self , startIndex = None , endIndex = None ):
174
- return self ._base .toPrintable ( startIndex , endIndex ).upper ()
166
+ def to_printable (self , start_index = None , end_index = None ):
167
+ return self ._base .to_printable ( start_index , end_index ).upper ()
0 commit comments