11SUPERSTRING_MINIMAL_LENGTH = 48
22
3+
34class SuperStringBase (object ):
45 def length (self ):
56 pass
67
78 def lower (self ):
89 if self .length () < SUPERSTRING_MINIMAL_LENGTH :
9- return SuperString (self .toPrintable ().lower ())
10+ return SuperString (self .to_printable ().lower ())
1011 return SuperStringLower (self )
1112
1213 def upper (self ):
1314 if self .length () < SUPERSTRING_MINIMAL_LENGTH :
14- return SuperString (self .toPrintable ().upper ())
15+ return SuperString (self .to_printable ().upper ())
1516 return SuperStringUpper (self )
1617
17- def characterAt (self , index ):
18+ def character_at (self , index ):
1819 pass
1920
20- def split (self , separator = " " ):
21+ def split (self , separator = " " ):
2122 result = []
2223 previous = 0
2324 i = 0
2425 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 ]:
3028 break
31- j = j + 1
32- if not gonext :
29+ else :
3330 result .append (self .substring (previous , i ))
3431 previous = i + len (separator )
3532 i = i + 1
3633 result .append (self .substring (previous ))
3734 return result
3835
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 ():
4641 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 )
5045
5146 def strip (self ):
5247 i = 0
53- while self .characterAt (i ) == ' ' :
48+ while self .character_at (i ) == ' ' :
5449 i = i + 1
55- startIndex = i
50+ start_index = i
5651 i = self .length () - 1
57- while self .characterAt (i ) == ' ' :
52+ while self .character_at (i ) == ' ' :
5853 i = i - 1
59- return self .substring (startIndex , i + 1 )
54+ return self .substring (start_index , i + 1 )
6055
61- def toPrintable (self , startIndex = None , endIndex = None ):
56+ def to_printable (self , start_index = None , end_index = None ):
6257 pass
6358
6459 def __len__ (self ):
@@ -70,35 +65,33 @@ def __add__(self, right):
7065 return SuperStringConcatenation (self , right )
7166
7267 def __str__ (self ):
73- return self .toPrintable ()
68+ return self .to_printable ()
7469
7570 def __getitem__ (self , key ):
7671 if isinstance (key , slice ):
7772 start = key .start if key .start > 0 else self .length () + key .start
7873 stop = key .stop if key .stop > 0 else self .length () + key .stop
7974 return self .substring (start , end_index = stop )
80- return self .characterAt (key )
75+ return self .character_at (key )
76+
8177
8278class SuperString (SuperStringBase ):
8379 def __init__ (self , content ):
8480 self ._content = content
8581
8682 def length (self ):
87- if not hasattr (self ,'_length' ):
83+ if not hasattr (self , '_length' ):
8884 self ._length = len (self ._content )
8985 return self ._length
9086
91- def characterAt (self , index ):
87+ def character_at (self , index ):
9288 return self ._content [index ]
9389
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+
10295
10396class SuperStringConcatenation (SuperStringBase ):
10497 def __init__ (self , left , right ):
@@ -108,44 +101,43 @@ def __init__(self, left, right):
108101 def length (self ):
109102 return self ._left .length () + self ._right .length ()
110103
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 :
114107 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+
128121
129122class SuperStringSubstring (SuperStringBase ):
130- def __init__ (self , base , startIndex , endIndex ):
123+ def __init__ (self , base , start_index , end_index ):
131124 self ._base = base
132- self ._startIndex = startIndex
133- self ._endIndex = endIndex
125+ self ._start_index = start_index
126+ self ._end_index = end_index
134127
135128 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 )
137133
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 )
140140
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 )
149141
150142class SuperStringLower (SuperStringBase ):
151143 def __init__ (self , base ):
@@ -154,11 +146,12 @@ def __init__(self, base):
154146 def length (self ):
155147 return self ._base .length ()
156148
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 ()
159154
160- def toPrintable (self , startIndex = None , endIndex = None ):
161- return self ._base .toPrintable (startIndex , endIndex ).lower ()
162155
163156class SuperStringUpper (SuperStringBase ):
164157 def __init__ (self , base ):
@@ -167,8 +160,8 @@ def __init__(self, base):
167160 def length (self ):
168161 return self ._base .length ()
169162
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 ()
172165
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