PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/string.py

http://github.com/IronLanguages/main
Python | 656 lines | 648 code | 3 blank | 5 comment | 1 complexity | c001953da166cc970c65e18f270a835c MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. """A collection of string operations (most are no longer used).
  2. Warning: most of the code you see here isn't normally used nowadays.
  3. Beginning with Python 1.6, many of these functions are implemented as
  4. methods on the standard string object. They used to be implemented by
  5. a built-in module called strop, but strop is now obsolete itself.
  6. Public module variables:
  7. whitespace -- a string containing all characters considered whitespace
  8. lowercase -- a string containing all characters considered lowercase letters
  9. uppercase -- a string containing all characters considered uppercase letters
  10. letters -- a string containing all characters considered letters
  11. digits -- a string containing all characters considered decimal digits
  12. hexdigits -- a string containing all characters considered hexadecimal digits
  13. octdigits -- a string containing all characters considered octal digits
  14. punctuation -- a string containing all characters considered punctuation
  15. printable -- a string containing all characters considered printable
  16. """
  17. # Some strings for ctype-style character classification
  18. whitespace = ' \t\n\r\v\f'
  19. lowercase = 'abcdefghijklmnopqrstuvwxyz'
  20. uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  21. letters = lowercase + uppercase
  22. ascii_lowercase = lowercase
  23. ascii_uppercase = uppercase
  24. ascii_letters = ascii_lowercase + ascii_uppercase
  25. digits = '0123456789'
  26. hexdigits = digits + 'abcdef' + 'ABCDEF'
  27. octdigits = '01234567'
  28. punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
  29. printable = digits + letters + punctuation + whitespace
  30. # Case conversion helpers
  31. # Use str to convert Unicode literal in case of -U
  32. l = map(chr, xrange(256))
  33. _idmap = str('').join(l)
  34. del l
  35. # Functions which aren't available as string methods.
  36. # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
  37. def capwords(s, sep=None):
  38. """capwords(s [,sep]) -> string
  39. Split the argument into words using split, capitalize each
  40. word using capitalize, and join the capitalized words using
  41. join. If the optional second argument sep is absent or None,
  42. runs of whitespace characters are replaced by a single space
  43. and leading and trailing whitespace are removed, otherwise
  44. sep is used to split and join the words.
  45. """
  46. return (sep or ' ').join(x.capitalize() for x in s.split(sep))
  47. # Construct a translation string
  48. _idmapL = None
  49. def maketrans(fromstr, tostr):
  50. """maketrans(frm, to) -> string
  51. Return a translation table (a string of 256 bytes long)
  52. suitable for use in string.translate. The strings frm and to
  53. must be of the same length.
  54. """
  55. if len(fromstr) != len(tostr):
  56. raise ValueError, "maketrans arguments must have same length"
  57. global _idmapL
  58. if not _idmapL:
  59. _idmapL = list(_idmap)
  60. L = _idmapL[:]
  61. fromstr = map(ord, fromstr)
  62. for i in range(len(fromstr)):
  63. L[fromstr[i]] = tostr[i]
  64. return ''.join(L)
  65. ####################################################################
  66. import re as _re
  67. class _multimap:
  68. """Helper class for combining multiple mappings.
  69. Used by .{safe_,}substitute() to combine the mapping and keyword
  70. arguments.
  71. """
  72. def __init__(self, primary, secondary):
  73. self._primary = primary
  74. self._secondary = secondary
  75. def __getitem__(self, key):
  76. try:
  77. return self._primary[key]
  78. except KeyError:
  79. return self._secondary[key]
  80. class _TemplateMetaclass(type):
  81. pattern = r"""
  82. %(delim)s(?:
  83. (?P<escaped>%(delim)s) | # Escape sequence of two delimiters
  84. (?P<named>%(id)s) | # delimiter and a Python identifier
  85. {(?P<braced>%(id)s)} | # delimiter and a braced identifier
  86. (?P<invalid>) # Other ill-formed delimiter exprs
  87. )
  88. """
  89. def __init__(cls, name, bases, dct):
  90. super(_TemplateMetaclass, cls).__init__(name, bases, dct)
  91. if 'pattern' in dct:
  92. pattern = cls.pattern
  93. else:
  94. pattern = _TemplateMetaclass.pattern % {
  95. 'delim' : _re.escape(cls.delimiter),
  96. 'id' : cls.idpattern,
  97. }
  98. cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
  99. class Template:
  100. """A string class for supporting $-substitutions."""
  101. __metaclass__ = _TemplateMetaclass
  102. delimiter = '$'
  103. idpattern = r'[_a-z][_a-z0-9]*'
  104. def __init__(self, template):
  105. self.template = template
  106. # Search for $$, $identifier, ${identifier}, and any bare $'s
  107. def _invalid(self, mo):
  108. i = mo.start('invalid')
  109. lines = self.template[:i].splitlines(True)
  110. if not lines:
  111. colno = 1
  112. lineno = 1
  113. else:
  114. colno = i - len(''.join(lines[:-1]))
  115. lineno = len(lines)
  116. raise ValueError('Invalid placeholder in string: line %d, col %d' %
  117. (lineno, colno))
  118. def substitute(*args, **kws):
  119. if not args:
  120. raise TypeError("descriptor 'substitute' of 'Template' object "
  121. "needs an argument")
  122. self, args = args[0], args[1:] # allow the "self" keyword be passed
  123. if len(args) > 1:
  124. raise TypeError('Too many positional arguments')
  125. if not args:
  126. mapping = kws
  127. elif kws:
  128. mapping = _multimap(kws, args[0])
  129. else:
  130. mapping = args[0]
  131. # Helper function for .sub()
  132. def convert(mo):
  133. # Check the most common path first.
  134. named = mo.group('named') or mo.group('braced')
  135. if named is not None:
  136. val = mapping[named]
  137. # We use this idiom instead of str() because the latter will
  138. # fail if val is a Unicode containing non-ASCII characters.
  139. return '%s' % (val,)
  140. if mo.group('escaped') is not None:
  141. return self.delimiter
  142. if mo.group('invalid') is not None:
  143. self._invalid(mo)
  144. raise ValueError('Unrecognized named group in pattern',
  145. self.pattern)
  146. return self.pattern.sub(convert, self.template)
  147. def safe_substitute(*args, **kws):
  148. if not args:
  149. raise TypeError("descriptor 'safe_substitute' of 'Template' object "
  150. "needs an argument")
  151. self, args = args[0], args[1:] # allow the "self" keyword be passed
  152. if len(args) > 1:
  153. raise TypeError('Too many positional arguments')
  154. if not args:
  155. mapping = kws
  156. elif kws:
  157. mapping = _multimap(kws, args[0])
  158. else:
  159. mapping = args[0]
  160. # Helper function for .sub()
  161. def convert(mo):
  162. named = mo.group('named') or mo.group('braced')
  163. if named is not None:
  164. try:
  165. # We use this idiom instead of str() because the latter
  166. # will fail if val is a Unicode containing non-ASCII
  167. return '%s' % (mapping[named],)
  168. except KeyError:
  169. return mo.group()
  170. if mo.group('escaped') is not None:
  171. return self.delimiter
  172. if mo.group('invalid') is not None:
  173. return mo.group()
  174. raise ValueError('Unrecognized named group in pattern',
  175. self.pattern)
  176. return self.pattern.sub(convert, self.template)
  177. ####################################################################
  178. # NOTE: Everything below here is deprecated. Use string methods instead.
  179. # This stuff will go away in Python 3.0.
  180. # Backward compatible names for exceptions
  181. index_error = ValueError
  182. atoi_error = ValueError
  183. atof_error = ValueError
  184. atol_error = ValueError
  185. # convert UPPER CASE letters to lower case
  186. def lower(s):
  187. """lower(s) -> string
  188. Return a copy of the string s converted to lowercase.
  189. """
  190. return s.lower()
  191. # Convert lower case letters to UPPER CASE
  192. def upper(s):
  193. """upper(s) -> string
  194. Return a copy of the string s converted to uppercase.
  195. """
  196. return s.upper()
  197. # Swap lower case letters and UPPER CASE
  198. def swapcase(s):
  199. """swapcase(s) -> string
  200. Return a copy of the string s with upper case characters
  201. converted to lowercase and vice versa.
  202. """
  203. return s.swapcase()
  204. # Strip leading and trailing tabs and spaces
  205. def strip(s, chars=None):
  206. """strip(s [,chars]) -> string
  207. Return a copy of the string s with leading and trailing
  208. whitespace removed.
  209. If chars is given and not None, remove characters in chars instead.
  210. If chars is unicode, S will be converted to unicode before stripping.
  211. """
  212. return s.strip(chars)
  213. # Strip leading tabs and spaces
  214. def lstrip(s, chars=None):
  215. """lstrip(s [,chars]) -> string
  216. Return a copy of the string s with leading whitespace removed.
  217. If chars is given and not None, remove characters in chars instead.
  218. """
  219. return s.lstrip(chars)
  220. # Strip trailing tabs and spaces
  221. def rstrip(s, chars=None):
  222. """rstrip(s [,chars]) -> string
  223. Return a copy of the string s with trailing whitespace removed.
  224. If chars is given and not None, remove characters in chars instead.
  225. """
  226. return s.rstrip(chars)
  227. # Split a string into a list of space/tab-separated words
  228. def split(s, sep=None, maxsplit=-1):
  229. """split(s [,sep [,maxsplit]]) -> list of strings
  230. Return a list of the words in the string s, using sep as the
  231. delimiter string. If maxsplit is given, splits at no more than
  232. maxsplit places (resulting in at most maxsplit+1 words). If sep
  233. is not specified or is None, any whitespace string is a separator.
  234. (split and splitfields are synonymous)
  235. """
  236. return s.split(sep, maxsplit)
  237. splitfields = split
  238. # Split a string into a list of space/tab-separated words
  239. def rsplit(s, sep=None, maxsplit=-1):
  240. """rsplit(s [,sep [,maxsplit]]) -> list of strings
  241. Return a list of the words in the string s, using sep as the
  242. delimiter string, starting at the end of the string and working
  243. to the front. If maxsplit is given, at most maxsplit splits are
  244. done. If sep is not specified or is None, any whitespace string
  245. is a separator.
  246. """
  247. return s.rsplit(sep, maxsplit)
  248. # Join fields with optional separator
  249. def join(words, sep = ' '):
  250. """join(list [,sep]) -> string
  251. Return a string composed of the words in list, with
  252. intervening occurrences of sep. The default separator is a
  253. single space.
  254. (joinfields and join are synonymous)
  255. """
  256. return sep.join(words)
  257. joinfields = join
  258. # Find substring, raise exception if not found
  259. def index(s, *args):
  260. """index(s, sub [,start [,end]]) -> int
  261. Like find but raises ValueError when the substring is not found.
  262. """
  263. return s.index(*args)
  264. # Find last substring, raise exception if not found
  265. def rindex(s, *args):
  266. """rindex(s, sub [,start [,end]]) -> int
  267. Like rfind but raises ValueError when the substring is not found.
  268. """
  269. return s.rindex(*args)
  270. # Count non-overlapping occurrences of substring
  271. def count(s, *args):
  272. """count(s, sub[, start[,end]]) -> int
  273. Return the number of occurrences of substring sub in string
  274. s[start:end]. Optional arguments start and end are
  275. interpreted as in slice notation.
  276. """
  277. return s.count(*args)
  278. # Find substring, return -1 if not found
  279. def find(s, *args):
  280. """find(s, sub [,start [,end]]) -> in
  281. Return the lowest index in s where substring sub is found,
  282. such that sub is contained within s[start,end]. Optional
  283. arguments start and end are interpreted as in slice notation.
  284. Return -1 on failure.
  285. """
  286. return s.find(*args)
  287. # Find last substring, return -1 if not found
  288. def rfind(s, *args):
  289. """rfind(s, sub [,start [,end]]) -> int
  290. Return the highest index in s where substring sub is found,
  291. such that sub is contained within s[start,end]. Optional
  292. arguments start and end are interpreted as in slice notation.
  293. Return -1 on failure.
  294. """
  295. return s.rfind(*args)
  296. # for a bit of speed
  297. _float = float
  298. _int = int
  299. _long = long
  300. # Convert string to float
  301. def atof(s):
  302. """atof(s) -> float
  303. Return the floating point number represented by the string s.
  304. """
  305. return _float(s)
  306. # Convert string to integer
  307. def atoi(s , base=10):
  308. """atoi(s [,base]) -> int
  309. Return the integer represented by the string s in the given
  310. base, which defaults to 10. The string s must consist of one
  311. or more digits, possibly preceded by a sign. If base is 0, it
  312. is chosen from the leading characters of s, 0 for octal, 0x or
  313. 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
  314. accepted.
  315. """
  316. return _int(s, base)
  317. # Convert string to long integer
  318. def atol(s, base=10):
  319. """atol(s [,base]) -> long
  320. Return the long integer represented by the string s in the
  321. given base, which defaults to 10. The string s must consist
  322. of one or more digits, possibly preceded by a sign. If base
  323. is 0, it is chosen from the leading characters of s, 0 for
  324. octal, 0x or 0X for hexadecimal. If base is 16, a preceding
  325. 0x or 0X is accepted. A trailing L or l is not accepted,
  326. unless base is 0.
  327. """
  328. return _long(s, base)
  329. # Left-justify a string
  330. def ljust(s, width, *args):
  331. """ljust(s, width[, fillchar]) -> string
  332. Return a left-justified version of s, in a field of the
  333. specified width, padded with spaces as needed. The string is
  334. never truncated. If specified the fillchar is used instead of spaces.
  335. """
  336. return s.ljust(width, *args)
  337. # Right-justify a string
  338. def rjust(s, width, *args):
  339. """rjust(s, width[, fillchar]) -> string
  340. Return a right-justified version of s, in a field of the
  341. specified width, padded with spaces as needed. The string is
  342. never truncated. If specified the fillchar is used instead of spaces.
  343. """
  344. return s.rjust(width, *args)
  345. # Center a string
  346. def center(s, width, *args):
  347. """center(s, width[, fillchar]) -> string
  348. Return a center version of s, in a field of the specified
  349. width. padded with spaces as needed. The string is never
  350. truncated. If specified the fillchar is used instead of spaces.
  351. """
  352. return s.center(width, *args)
  353. # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
  354. # Decadent feature: the argument may be a string or a number
  355. # (Use of this is deprecated; it should be a string as with ljust c.s.)
  356. def zfill(x, width):
  357. """zfill(x, width) -> string
  358. Pad a numeric string x with zeros on the left, to fill a field
  359. of the specified width. The string x is never truncated.
  360. """
  361. if not isinstance(x, basestring):
  362. x = repr(x)
  363. return x.zfill(width)
  364. # Expand tabs in a string.
  365. # Doesn't take non-printing chars into account, but does understand \n.
  366. def expandtabs(s, tabsize=8):
  367. """expandtabs(s [,tabsize]) -> string
  368. Return a copy of the string s with all tab characters replaced
  369. by the appropriate number of spaces, depending on the current
  370. column, and the tabsize (default 8).
  371. """
  372. return s.expandtabs(tabsize)
  373. # Character translation through look-up table.
  374. def translate(s, table, deletions=""):
  375. """translate(s,table [,deletions]) -> string
  376. Return a copy of the string s, where all characters occurring
  377. in the optional argument deletions are removed, and the
  378. remaining characters have been mapped through the given
  379. translation table, which must be a string of length 256. The
  380. deletions argument is not allowed for Unicode strings.
  381. """
  382. if deletions or table is None:
  383. return s.translate(table, deletions)
  384. else:
  385. # Add s[:0] so that if s is Unicode and table is an 8-bit string,
  386. # table is converted to Unicode. This means that table *cannot*
  387. # be a dictionary -- for that feature, use u.translate() directly.
  388. return s.translate(table + s[:0])
  389. # Capitalize a string, e.g. "aBc dEf" -> "Abc def".
  390. def capitalize(s):
  391. """capitalize(s) -> string
  392. Return a copy of the string s with only its first character
  393. capitalized.
  394. """
  395. return s.capitalize()
  396. # Substring replacement (global)
  397. def replace(s, old, new, maxreplace=-1):
  398. """replace (str, old, new[, maxreplace]) -> string
  399. Return a copy of string str with all occurrences of substring
  400. old replaced by new. If the optional argument maxreplace is
  401. given, only the first maxreplace occurrences are replaced.
  402. """
  403. return s.replace(old, new, maxreplace)
  404. # Try importing optional built-in module "strop" -- if it exists,
  405. # it redefines some string operations that are 100-1000 times faster.
  406. # It also defines values for whitespace, lowercase and uppercase
  407. # that match <ctype.h>'s definitions.
  408. try:
  409. from strop import maketrans, lowercase, uppercase, whitespace
  410. letters = lowercase + uppercase
  411. except ImportError:
  412. pass # Use the original versions
  413. ########################################################################
  414. # the Formatter class
  415. # see PEP 3101 for details and purpose of this class
  416. # The hard parts are reused from the C implementation. They're exposed as "_"
  417. # prefixed methods of str and unicode.
  418. # The overall parser is implemented in str._formatter_parser.
  419. # The field name parser is implemented in str._formatter_field_name_split
  420. class Formatter(object):
  421. def format(*args, **kwargs):
  422. if not args:
  423. raise TypeError("descriptor 'format' of 'Formatter' object "
  424. "needs an argument")
  425. self, args = args[0], args[1:] # allow the "self" keyword be passed
  426. try:
  427. format_string, args = args[0], args[1:] # allow the "format_string" keyword be passed
  428. except IndexError:
  429. if 'format_string' in kwargs:
  430. format_string = kwargs.pop('format_string')
  431. else:
  432. raise TypeError("format() missing 1 required positional "
  433. "argument: 'format_string'")
  434. return self.vformat(format_string, args, kwargs)
  435. def vformat(self, format_string, args, kwargs):
  436. used_args = set()
  437. result = self._vformat(format_string, args, kwargs, used_args, 2)
  438. self.check_unused_args(used_args, args, kwargs)
  439. return result
  440. def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
  441. if recursion_depth < 0:
  442. raise ValueError('Max string recursion exceeded')
  443. result = []
  444. for literal_text, field_name, format_spec, conversion in \
  445. self.parse(format_string):
  446. # output the literal text
  447. if literal_text:
  448. result.append(literal_text)
  449. # if there's a field, output it
  450. if field_name is not None:
  451. # this is some markup, find the object and do
  452. # the formatting
  453. # given the field_name, find the object it references
  454. # and the argument it came from
  455. obj, arg_used = self.get_field(field_name, args, kwargs)
  456. used_args.add(arg_used)
  457. # do any conversion on the resulting object
  458. obj = self.convert_field(obj, conversion)
  459. # expand the format spec, if needed
  460. format_spec = self._vformat(format_spec, args, kwargs,
  461. used_args, recursion_depth-1)
  462. # format the object and append to the result
  463. result.append(self.format_field(obj, format_spec))
  464. return ''.join(result)
  465. def get_value(self, key, args, kwargs):
  466. if isinstance(key, (int, long)):
  467. return args[key]
  468. else:
  469. return kwargs[key]
  470. def check_unused_args(self, used_args, args, kwargs):
  471. pass
  472. def format_field(self, value, format_spec):
  473. return format(value, format_spec)
  474. def convert_field(self, value, conversion):
  475. # do any conversion on the resulting object
  476. if conversion is None:
  477. return value
  478. elif conversion == 's':
  479. return str(value)
  480. elif conversion == 'r':
  481. return repr(value)
  482. raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
  483. # returns an iterable that contains tuples of the form:
  484. # (literal_text, field_name, format_spec, conversion)
  485. # literal_text can be zero length
  486. # field_name can be None, in which case there's no
  487. # object to format and output
  488. # if field_name is not None, it is looked up, formatted
  489. # with format_spec and conversion and then used
  490. def parse(self, format_string):
  491. return format_string._formatter_parser()
  492. # given a field_name, find the object it references.
  493. # field_name: the field being looked up, e.g. "0.name"
  494. # or "lookup[3]"
  495. # used_args: a set of which args have been used
  496. # args, kwargs: as passed in to vformat
  497. def get_field(self, field_name, args, kwargs):
  498. first, rest = field_name._formatter_field_name_split()
  499. obj = self.get_value(first, args, kwargs)
  500. # loop through the rest of the field_name, doing
  501. # getattr or getitem as needed
  502. for is_attr, i in rest:
  503. if is_attr:
  504. obj = getattr(obj, i)
  505. else:
  506. obj = obj[i]
  507. return obj, first