strmisc.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2016 Joey Payne
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains various string utility routines that are uncommonly
  10. ## used in comparison to the ones in `strutils <strutils.html>`_.
  11. import std/strutils
  12. func expandTabs*(s: string, tabSize: int = 8): string =
  13. ## Expands tab characters in `s`, replacing them by spaces.
  14. ##
  15. ## The amount of inserted spaces for each tab character is the difference
  16. ## between the current column number and the next tab position. Tab positions
  17. ## occur every `tabSize` characters.
  18. ## The column number starts at 0 and is increased with every single character
  19. ## and inserted space, except for newline, which resets the column number
  20. ## back to 0.
  21. runnableExamples:
  22. doAssert expandTabs("\t", 4) == " "
  23. doAssert expandTabs("\tfoo\t", 4) == " foo "
  24. doAssert expandTabs("a\tb\n\txy\t", 3) == "a b\n xy "
  25. result = newStringOfCap(s.len + s.len shr 2)
  26. var pos = 0
  27. template addSpaces(n) =
  28. for j in 0 ..< n:
  29. result.add(' ')
  30. pos += 1
  31. for i in 0 ..< len(s):
  32. let c = s[i]
  33. if c == '\t':
  34. let
  35. denominator = if tabSize > 0: tabSize else: 1
  36. numSpaces = tabSize - pos mod denominator
  37. addSpaces(numSpaces)
  38. else:
  39. result.add(c)
  40. pos += 1
  41. if c == '\l':
  42. pos = 0
  43. func partition*(s: string, sep: string,
  44. right: bool = false): (string, string, string) =
  45. ## Splits the string at the first (if `right` is false)
  46. ## or last (if `right` is true) occurrence of `sep` into a 3-tuple.
  47. ##
  48. ## Returns a 3-tuple of strings, `(beforeSep, sep, afterSep)` or
  49. ## `(s, "", "")` if `sep` is not found and `right` is false or
  50. ## `("", "", s)` if `sep` is not found and `right` is true.
  51. ##
  52. ## **See also:**
  53. ## * `rpartition proc <#rpartition,string,string>`_
  54. runnableExamples:
  55. doAssert partition("foo:bar:baz", ":") == ("foo", ":", "bar:baz")
  56. doAssert partition("foo:bar:baz", ":", right = true) == ("foo:bar", ":", "baz")
  57. doAssert partition("foobar", ":") == ("foobar", "", "")
  58. doAssert partition("foobar", ":", right = true) == ("", "", "foobar")
  59. let position = if right: s.rfind(sep) else: s.find(sep)
  60. if position != -1:
  61. return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
  62. return if right: ("", "", s) else: (s, "", "")
  63. func rpartition*(s: string, sep: string): (string, string, string) =
  64. ## Splits the string at the last occurrence of `sep` into a 3-tuple.
  65. ##
  66. ## Returns a 3-tuple of strings, `(beforeSep, sep, afterSep)` or
  67. ## `("", "", s)` if `sep` is not found. This is the same as
  68. ## `partition(s, sep, right = true)`.
  69. ##
  70. ## **See also:**
  71. ## * `partition proc <#partition,string,string,bool>`_
  72. runnableExamples:
  73. doAssert rpartition("foo:bar:baz", ":") == ("foo:bar", ":", "baz")
  74. doAssert rpartition("foobar", ":") == ("", "", "foobar")
  75. partition(s, sep, right = true)