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