paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | String objects. |
paul@6 | 5 | |
paul@514 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@303 | 22 | from __builtins__.operator import _negate |
paul@459 | 23 | from __builtins__.sequence import hashable, itemaccess |
paul@384 | 24 | from __builtins__.types import check_int |
paul@583 | 25 | from native import int_new, str_add, str_lt, str_gt, str_eq, str_ord, \ |
paul@583 | 26 | str_substr |
paul@6 | 27 | |
paul@515 | 28 | WHITESPACE = (" ", "\f", "\n", "\r", "\t") |
paul@515 | 29 | |
paul@459 | 30 | class basestring(hashable): |
paul@289 | 31 | |
paul@289 | 32 | "The base class for all strings." |
paul@289 | 33 | |
paul@390 | 34 | def __init__(self, other=None): |
paul@6 | 35 | |
paul@390 | 36 | "Initialise the string, perhaps from 'other'." |
paul@6 | 37 | |
paul@289 | 38 | # Note the __data__ member. Since strings are either initialised from |
paul@289 | 39 | # literals or converted using routines defined for other types, no form |
paul@289 | 40 | # of actual initialisation is performed here. |
paul@6 | 41 | |
paul@583 | 42 | # Note the __key__ member. This is also initialised statically. Where |
paul@583 | 43 | # a string is the same as an attribute name, the __key__ member contains |
paul@583 | 44 | # attribute position and code details. |
paul@583 | 45 | |
paul@390 | 46 | # NOTE: Cannot perform "other and other.__data__ or None" since the |
paul@390 | 47 | # NOTE: __data__ attribute is not a normal attribute. |
paul@390 | 48 | |
paul@390 | 49 | if other: |
paul@390 | 50 | self.__data__ = other.__data__ |
paul@583 | 51 | self.__key__ = other.__key__ |
paul@583 | 52 | self.__size__ = other.__size__ |
paul@390 | 53 | else: |
paul@390 | 54 | self.__data__ = None |
paul@390 | 55 | self.__key__ = None |
paul@583 | 56 | self.__size__ = None |
paul@360 | 57 | |
paul@518 | 58 | # Internal methods. |
paul@300 | 59 | |
paul@583 | 60 | def _binary_op(self, op, other, sizes=False): |
paul@303 | 61 | |
paul@396 | 62 | "Perform 'op' on this object and 'other' if appropriate." |
paul@396 | 63 | |
paul@396 | 64 | # Refuse to operate on specialisations of this class. |
paul@396 | 65 | |
paul@396 | 66 | if self.__class__ is not other.__class__: |
paul@396 | 67 | return NotImplemented |
paul@396 | 68 | |
paul@396 | 69 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 70 | |
paul@583 | 71 | elif sizes: |
paul@583 | 72 | return op(self.__data__, other.__data__, self.__size__, other.__size__) |
paul@396 | 73 | else: |
paul@396 | 74 | return op(self.__data__, other.__data__) |
paul@303 | 75 | |
paul@583 | 76 | def _binary_op_rev(self, op, other, sizes=False): |
paul@396 | 77 | |
paul@396 | 78 | "Perform 'op' on 'other' and this object if appropriate." |
paul@396 | 79 | |
paul@396 | 80 | # Refuse to operate on specialisations of this class. |
paul@396 | 81 | |
paul@396 | 82 | if self.__class__ is not other.__class__: |
paul@396 | 83 | return NotImplemented |
paul@396 | 84 | |
paul@396 | 85 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 86 | |
paul@583 | 87 | elif sizes: |
paul@583 | 88 | return op(other.__data__, self.__data__, other.__size__, self.__size__) |
paul@303 | 89 | else: |
paul@396 | 90 | return op(other.__data__, self.__data__) |
paul@303 | 91 | |
paul@518 | 92 | def _quote(self, quote): |
paul@518 | 93 | |
paul@518 | 94 | "Return a quoted representation of this string." |
paul@518 | 95 | |
paul@518 | 96 | b = buffer([quote]) |
paul@518 | 97 | i = last = 0 |
paul@518 | 98 | end = self.__len__() |
paul@518 | 99 | |
paul@518 | 100 | while i < end: |
paul@518 | 101 | c = self[i] |
paul@519 | 102 | |
paul@519 | 103 | # Handle quotes before anything else. |
paul@519 | 104 | |
paul@519 | 105 | if c == quote: |
paul@519 | 106 | b.append("\\") |
paul@519 | 107 | b.append(quote) |
paul@519 | 108 | i += 1 |
paul@519 | 109 | last = i |
paul@519 | 110 | continue |
paul@518 | 111 | |
paul@518 | 112 | # Extended unquoted text. |
paul@518 | 113 | |
paul@519 | 114 | n = ord(c) |
paul@519 | 115 | |
paul@518 | 116 | if 32 <= n < 128: |
paul@518 | 117 | i += 1 |
paul@518 | 118 | continue |
paul@518 | 119 | |
paul@518 | 120 | # Before quoting, emit unquoted text. |
paul@518 | 121 | |
paul@518 | 122 | b.append(self[last:i]) |
paul@518 | 123 | |
paul@518 | 124 | # Add quoted value. |
paul@518 | 125 | |
paul@519 | 126 | if c == "\t": |
paul@518 | 127 | b.append("\\t") |
paul@518 | 128 | elif c == "\n": |
paul@518 | 129 | b.append("\\n") |
paul@518 | 130 | elif c == "\r": |
paul@518 | 131 | b.append("\\r") |
paul@518 | 132 | else: |
paul@536 | 133 | self._quote_value(b, n) |
paul@518 | 134 | |
paul@518 | 135 | i += 1 |
paul@518 | 136 | last = i |
paul@518 | 137 | |
paul@518 | 138 | # Emit remaining unquoted text. |
paul@518 | 139 | |
paul@518 | 140 | b.append(self[last:]) |
paul@518 | 141 | b.append(quote) |
paul@518 | 142 | return str(b) |
paul@518 | 143 | |
paul@536 | 144 | def _quote_value(self, b, n): |
paul@536 | 145 | |
paul@536 | 146 | "Append to 'b' the quoted form of 'n'." |
paul@536 | 147 | |
paul@536 | 148 | if n < 0: |
paul@536 | 149 | n += 256 |
paul@536 | 150 | b.append("\\x") |
paul@536 | 151 | x = hex(n, "") |
paul@536 | 152 | if len(x) < 2: |
paul@536 | 153 | b.append("0") |
paul@536 | 154 | b.append(x) |
paul@536 | 155 | |
paul@518 | 156 | def bytelength(self): |
paul@518 | 157 | |
paul@518 | 158 | "Return the number of bytes in this string." |
paul@518 | 159 | |
paul@583 | 160 | return int_new(self.__size__) |
paul@518 | 161 | |
paul@518 | 162 | # General type methods. |
paul@518 | 163 | |
paul@518 | 164 | def __bool__(self): |
paul@518 | 165 | |
paul@518 | 166 | "Return whether the string provides any data." |
paul@518 | 167 | |
paul@583 | 168 | return int_new(self.__size__).__bool__() |
paul@518 | 169 | |
paul@518 | 170 | def __contains__(self, value): |
paul@518 | 171 | |
paul@518 | 172 | "Return whether this string contains 'value'." |
paul@518 | 173 | |
paul@518 | 174 | return self.find(value) != -1 |
paul@518 | 175 | |
paul@518 | 176 | def __hash__(self): |
paul@518 | 177 | |
paul@518 | 178 | "Return a value for hashing purposes." |
paul@518 | 179 | |
paul@518 | 180 | return self._hashvalue(ord) |
paul@518 | 181 | |
paul@518 | 182 | __len__ = bytelength |
paul@518 | 183 | |
paul@518 | 184 | def __repr__(self): |
paul@518 | 185 | |
paul@518 | 186 | "Return a program representation." |
paul@518 | 187 | |
paul@518 | 188 | return self._quote('"') |
paul@518 | 189 | |
paul@518 | 190 | def __str__(self): |
paul@518 | 191 | |
paul@518 | 192 | "Return a string representation." |
paul@518 | 193 | |
paul@518 | 194 | return self |
paul@518 | 195 | |
paul@518 | 196 | # Operator methods. |
paul@518 | 197 | |
paul@6 | 198 | def __iadd__(self, other): |
paul@289 | 199 | |
paul@303 | 200 | "Return a string combining this string with 'other'." |
paul@289 | 201 | |
paul@583 | 202 | return self._binary_op(str_add, other, True) |
paul@6 | 203 | |
paul@396 | 204 | __add__ = __iadd__ |
paul@396 | 205 | |
paul@396 | 206 | def __radd__(self, other): |
paul@396 | 207 | |
paul@396 | 208 | "Return a string combining this string with 'other'." |
paul@396 | 209 | |
paul@583 | 210 | return self._binary_op_rev(str_add, other, True) |
paul@6 | 211 | |
paul@518 | 212 | def __mod__(self, other): pass |
paul@518 | 213 | def __rmod__(self, other): pass |
paul@518 | 214 | |
paul@514 | 215 | def __mul__(self, other): |
paul@514 | 216 | |
paul@514 | 217 | "Multiply the string by 'other'." |
paul@514 | 218 | |
paul@514 | 219 | b = buffer() |
paul@514 | 220 | |
paul@514 | 221 | while other > 0: |
paul@514 | 222 | b.append(self) |
paul@514 | 223 | other -= 1 |
paul@514 | 224 | |
paul@514 | 225 | return str(b) |
paul@514 | 226 | |
paul@514 | 227 | __rmul__ = __mul__ |
paul@514 | 228 | |
paul@518 | 229 | # Comparison methods. |
paul@518 | 230 | |
paul@518 | 231 | def __eq__(self, other): |
paul@518 | 232 | |
paul@518 | 233 | "Return whether this string is equal to 'other'." |
paul@6 | 234 | |
paul@518 | 235 | return self._binary_op(str_eq, other) |
paul@518 | 236 | |
paul@518 | 237 | def __ge__(self, other): |
paul@289 | 238 | |
paul@518 | 239 | "Return whether this string is greater than or equal to 'other'." |
paul@289 | 240 | |
paul@518 | 241 | return _negate(self.__lt__(other)) |
paul@6 | 242 | |
paul@6 | 243 | def __gt__(self, other): |
paul@289 | 244 | |
paul@303 | 245 | "Return whether this string is greater than 'other'." |
paul@289 | 246 | |
paul@356 | 247 | return self._binary_op(str_gt, other) |
paul@6 | 248 | |
paul@6 | 249 | def __le__(self, other): |
paul@289 | 250 | |
paul@303 | 251 | "Return whether this string is less than or equal to 'other'." |
paul@289 | 252 | |
paul@6 | 253 | return _negate(self.__gt__(other)) |
paul@6 | 254 | |
paul@518 | 255 | def __lt__(self, other): |
paul@6 | 256 | |
paul@518 | 257 | "Return whether this string is less than 'other'." |
paul@289 | 258 | |
paul@518 | 259 | return self._binary_op(str_lt, other) |
paul@6 | 260 | |
paul@6 | 261 | def __ne__(self, other): |
paul@289 | 262 | |
paul@303 | 263 | "Return whether this string is not equal to 'other'." |
paul@289 | 264 | |
paul@6 | 265 | return _negate(self.__eq__(other)) |
paul@6 | 266 | |
paul@518 | 267 | # String-specific methods. |
paul@435 | 268 | |
paul@534 | 269 | def __ord__(self): |
paul@534 | 270 | |
paul@534 | 271 | "Return the value of the string, if only a single character." |
paul@534 | 272 | |
paul@534 | 273 | if self.__len__() == 1: |
paul@534 | 274 | return str_ord(self.__data__) |
paul@534 | 275 | else: |
paul@534 | 276 | raise ValueError, self |
paul@534 | 277 | |
paul@413 | 278 | def endswith(self, s): |
paul@413 | 279 | |
paul@413 | 280 | "Return whether this string ends with 's'." |
paul@413 | 281 | |
paul@413 | 282 | return self[-s.__len__():] == s |
paul@413 | 283 | |
paul@413 | 284 | def find(self, sub, start=None, end=None): |
paul@413 | 285 | |
paul@413 | 286 | """ |
paul@515 | 287 | Find 'sub' in the string if it occurs from or after the 'start' position |
paul@515 | 288 | (or 0, if omitted) and before the 'end' position (or the end of the |
paul@515 | 289 | string, if omitted), returning the earliest occurrence or -1 if 'sub' is |
paul@515 | 290 | not present. |
paul@413 | 291 | """ |
paul@413 | 292 | |
paul@413 | 293 | sublen = sub.__len__() |
paul@413 | 294 | |
paul@515 | 295 | if end is None: |
paul@515 | 296 | end = self.__len__() |
paul@413 | 297 | |
paul@517 | 298 | end -= sublen |
paul@517 | 299 | |
paul@517 | 300 | i = start or 0 |
paul@517 | 301 | |
paul@517 | 302 | while i <= end: |
paul@413 | 303 | if sub == self[i:i+sublen]: |
paul@413 | 304 | return i |
paul@413 | 305 | i += 1 |
paul@413 | 306 | |
paul@413 | 307 | return -1 |
paul@413 | 308 | |
paul@413 | 309 | def index(self, sub, start=None, end=None): |
paul@413 | 310 | |
paul@413 | 311 | """ |
paul@413 | 312 | Find 'sub' in the string, starting at 'start' (or 0, if omitted), ending |
paul@413 | 313 | at 'end' (or the end of the string, if omitted), raising ValueError if |
paul@413 | 314 | 'sub' is not present. |
paul@413 | 315 | """ |
paul@413 | 316 | |
paul@413 | 317 | i = self.find(sub, start, end) |
paul@413 | 318 | |
paul@413 | 319 | if i == -1: |
paul@413 | 320 | raise ValueError(sub) |
paul@413 | 321 | else: |
paul@413 | 322 | return i |
paul@342 | 323 | |
paul@342 | 324 | def join(self, l): |
paul@342 | 325 | |
paul@342 | 326 | "Join the elements in 'l' with this string." |
paul@342 | 327 | |
paul@342 | 328 | # Empty strings just cause the list elements to be concatenated. |
paul@342 | 329 | |
paul@342 | 330 | if not self.__bool__(): |
paul@342 | 331 | return str(buffer(l)) |
paul@342 | 332 | |
paul@342 | 333 | # Non-empty strings join the elements together in a buffer. |
paul@342 | 334 | |
paul@342 | 335 | b = buffer() |
paul@342 | 336 | first = True |
paul@342 | 337 | |
paul@342 | 338 | for s in l: |
paul@342 | 339 | if first: |
paul@342 | 340 | first = False |
paul@342 | 341 | else: |
paul@342 | 342 | b.append(self) |
paul@342 | 343 | b.append(s) |
paul@342 | 344 | |
paul@342 | 345 | return str(b) |
paul@342 | 346 | |
paul@6 | 347 | def lower(self): pass |
paul@517 | 348 | |
paul@517 | 349 | def lstrip(self, chars=None): |
paul@517 | 350 | |
paul@517 | 351 | """ |
paul@517 | 352 | Strip any of the given 'chars' from the start of the string, or strip |
paul@517 | 353 | whitespace characters is 'chars' is omitted or None. |
paul@517 | 354 | """ |
paul@517 | 355 | |
paul@517 | 356 | if chars is not None and not chars: |
paul@517 | 357 | return self |
paul@517 | 358 | |
paul@517 | 359 | i = 0 |
paul@517 | 360 | end = self.__len__() |
paul@517 | 361 | |
paul@517 | 362 | while i < end and self[i] in (chars or WHITESPACE): |
paul@517 | 363 | i += 1 |
paul@517 | 364 | |
paul@517 | 365 | return self[i:] |
paul@517 | 366 | |
paul@6 | 367 | def replace(self, old, new, count=None): pass |
paul@517 | 368 | |
paul@515 | 369 | def rfind(self, sub, start=None, end=None): |
paul@515 | 370 | |
paul@515 | 371 | """ |
paul@515 | 372 | Find 'sub' in the string if it occurs from or after the 'start' position |
paul@515 | 373 | (or 0, if omitted) and before the 'end' position (or the end of the |
paul@515 | 374 | string, if omitted), returning the latest occurrence or -1 if 'sub' is |
paul@515 | 375 | not present. |
paul@515 | 376 | """ |
paul@515 | 377 | |
paul@515 | 378 | sublen = sub.__len__() |
paul@515 | 379 | |
paul@515 | 380 | start = start or 0 |
paul@515 | 381 | |
paul@515 | 382 | if end is None: |
paul@515 | 383 | end = self.__len__() |
paul@515 | 384 | |
paul@515 | 385 | i = end - sublen |
paul@515 | 386 | |
paul@515 | 387 | while i >= start: |
paul@515 | 388 | if sub == self[i:i+sublen]: |
paul@515 | 389 | return i |
paul@515 | 390 | i -= 1 |
paul@515 | 391 | |
paul@515 | 392 | return -1 |
paul@515 | 393 | |
paul@517 | 394 | def rsplit(self, sep=None, maxsplit=None): |
paul@517 | 395 | |
paul@517 | 396 | """ |
paul@517 | 397 | Split the string using the given 'sep' as separator (or any whitespace |
paul@517 | 398 | character if omitted or specified as None), splitting at most 'maxsplit' |
paul@517 | 399 | times (or as many times as is possible if omitted or specified as None). |
paul@517 | 400 | Where 'maxsplit' is given, the number of split points is counted from |
paul@517 | 401 | the end of the string. |
paul@517 | 402 | """ |
paul@517 | 403 | |
paul@517 | 404 | if not maxsplit: |
paul@517 | 405 | return self.split(sep, maxsplit) |
paul@517 | 406 | |
paul@517 | 407 | if sep is not None and not sep: |
paul@517 | 408 | raise ValueError, sep |
paul@517 | 409 | |
paul@517 | 410 | seplen = sep and len(sep) or 1 |
paul@517 | 411 | start = seplen |
paul@517 | 412 | splits = 0 |
paul@517 | 413 | |
paul@517 | 414 | l = [] |
paul@517 | 415 | i = last = self.__len__() |
paul@517 | 416 | |
paul@517 | 417 | while i >= start and (maxsplit is None or splits < maxsplit): |
paul@517 | 418 | |
paul@517 | 419 | # Find any specified separator. |
paul@517 | 420 | |
paul@517 | 421 | if sep and self[i-seplen:i] == sep: |
paul@517 | 422 | l.insert(0, self[i:last]) |
paul@517 | 423 | i -= seplen |
paul@517 | 424 | last = i |
paul@517 | 425 | splits += 1 |
paul@517 | 426 | |
paul@517 | 427 | # Find any whitespace character and skip adjacent characters. |
paul@517 | 428 | |
paul@517 | 429 | elif not sep and self[i-1] in WHITESPACE: |
paul@517 | 430 | l.insert(0, self[i:last]) |
paul@517 | 431 | while i > start: |
paul@517 | 432 | i -= 1 |
paul@517 | 433 | if self[i-1] not in WHITESPACE: |
paul@517 | 434 | break |
paul@517 | 435 | else: |
paul@517 | 436 | break |
paul@517 | 437 | last = i |
paul@517 | 438 | splits += 1 |
paul@517 | 439 | |
paul@517 | 440 | # Check the next character. |
paul@517 | 441 | |
paul@517 | 442 | else: |
paul@517 | 443 | i -= 1 |
paul@517 | 444 | |
paul@517 | 445 | l.insert(0, self[:last]) |
paul@517 | 446 | return l |
paul@517 | 447 | |
paul@517 | 448 | def rstrip(self, chars=None): |
paul@517 | 449 | |
paul@517 | 450 | """ |
paul@517 | 451 | Strip any of the given 'chars' from the end of the string, or strip |
paul@517 | 452 | whitespace characters is 'chars' is omitted or None. |
paul@517 | 453 | """ |
paul@517 | 454 | |
paul@517 | 455 | if chars is not None and not chars: |
paul@517 | 456 | return self |
paul@517 | 457 | |
paul@517 | 458 | i = self.__len__() - 1 |
paul@517 | 459 | |
paul@517 | 460 | while i >= 0 and self[i] in (chars or WHITESPACE): |
paul@517 | 461 | i -= 1 |
paul@517 | 462 | |
paul@517 | 463 | return self[:i+1] |
paul@515 | 464 | |
paul@515 | 465 | def split(self, sep=None, maxsplit=None): |
paul@515 | 466 | |
paul@515 | 467 | """ |
paul@515 | 468 | Split the string using the given 'sep' as separator (or any whitespace |
paul@515 | 469 | character if omitted or specified as None), splitting at most 'maxsplit' |
paul@515 | 470 | times (or as many times as is possible if omitted or specified as None). |
paul@517 | 471 | Where 'maxsplit' is given, the number of split points is counted from |
paul@517 | 472 | the start of the string. |
paul@515 | 473 | """ |
paul@515 | 474 | |
paul@515 | 475 | if sep is not None and not sep: |
paul@515 | 476 | raise ValueError, sep |
paul@515 | 477 | |
paul@517 | 478 | if maxsplit is not None and not maxsplit: |
paul@517 | 479 | return [self] |
paul@517 | 480 | |
paul@517 | 481 | seplen = sep and len(sep) or 1 |
paul@517 | 482 | end = self.__len__() - seplen |
paul@515 | 483 | splits = 0 |
paul@515 | 484 | |
paul@515 | 485 | l = [] |
paul@515 | 486 | i = last = 0 |
paul@515 | 487 | |
paul@517 | 488 | while i <= end and (maxsplit is None or splits < maxsplit): |
paul@515 | 489 | |
paul@515 | 490 | # Find any specified separator. |
paul@515 | 491 | |
paul@515 | 492 | if sep and self[i:i+seplen] == sep: |
paul@515 | 493 | l.append(self[last:i]) |
paul@515 | 494 | i += seplen |
paul@515 | 495 | last = i |
paul@515 | 496 | splits += 1 |
paul@515 | 497 | |
paul@515 | 498 | # Find any whitespace character and skip adjacent characters. |
paul@515 | 499 | |
paul@515 | 500 | elif not sep and self[i] in WHITESPACE: |
paul@515 | 501 | l.append(self[last:i]) |
paul@515 | 502 | while i < end: |
paul@515 | 503 | i += 1 |
paul@515 | 504 | if self[i] not in WHITESPACE: |
paul@515 | 505 | break |
paul@515 | 506 | else: |
paul@515 | 507 | break |
paul@515 | 508 | last = i |
paul@515 | 509 | splits += 1 |
paul@515 | 510 | |
paul@515 | 511 | # Check the next character. |
paul@515 | 512 | |
paul@515 | 513 | else: |
paul@515 | 514 | i += 1 |
paul@515 | 515 | |
paul@515 | 516 | l.append(self[last:]) |
paul@515 | 517 | return l |
paul@515 | 518 | |
paul@6 | 519 | def splitlines(self, keepends=False): pass |
paul@413 | 520 | |
paul@413 | 521 | def startswith(self, s): |
paul@413 | 522 | |
paul@413 | 523 | "Return whether this string starts with 's'." |
paul@413 | 524 | |
paul@413 | 525 | return self[:s.__len__()] == s |
paul@413 | 526 | |
paul@517 | 527 | def strip(self, chars=None): |
paul@517 | 528 | |
paul@517 | 529 | """ |
paul@517 | 530 | Strip any of the given 'chars' from the start and end of the string, or |
paul@517 | 531 | strip whitespace characters is 'chars' is omitted or None. |
paul@517 | 532 | """ |
paul@517 | 533 | |
paul@517 | 534 | return self.lstrip(chars).rstrip(chars) |
paul@517 | 535 | |
paul@6 | 536 | def upper(self): pass |
paul@6 | 537 | |
paul@431 | 538 | class string(basestring): |
paul@431 | 539 | |
paul@431 | 540 | "A plain string of bytes." |
paul@431 | 541 | |
paul@292 | 542 | # Special implementation methods. |
paul@292 | 543 | |
paul@292 | 544 | def __get_single_item__(self, index): |
paul@292 | 545 | |
paul@292 | 546 | "Return the item at the normalised (positive) 'index'." |
paul@292 | 547 | |
paul@292 | 548 | self._check_index(index) |
paul@384 | 549 | return str_substr(self.__data__, index, index + 1, 1) |
paul@384 | 550 | |
paul@384 | 551 | def __get_multiple_items__(self, start, end, step): |
paul@384 | 552 | |
paul@384 | 553 | """ |
paul@384 | 554 | Return items from 'start' until (but excluding) 'end', at 'step' |
paul@384 | 555 | intervals. |
paul@384 | 556 | """ |
paul@384 | 557 | |
paul@520 | 558 | if start == end: |
paul@520 | 559 | return "" |
paul@520 | 560 | |
paul@384 | 561 | check_int(step) |
paul@384 | 562 | |
paul@384 | 563 | if step == 0: |
paul@384 | 564 | raise ValueError(step) |
paul@384 | 565 | |
paul@520 | 566 | l = get_using(basestring.__get_multiple_items__, self)(start, end, step) |
paul@520 | 567 | return "".join(l) |
paul@292 | 568 | |
paul@188 | 569 | def str(obj): |
paul@188 | 570 | |
paul@188 | 571 | "Return the string representation of 'obj'." |
paul@188 | 572 | |
paul@248 | 573 | # Class attributes of instances provide __str__. |
paul@248 | 574 | |
paul@274 | 575 | return obj.__str__() |
paul@188 | 576 | |
paul@6 | 577 | # vim: tabstop=4 expandtab shiftwidth=4 |