strmisc.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 `strutils <strutils.html>`_.
  11. import strutils
  12. proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect.} =
  13. ## Expand 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("\tfoo\tbar", 4) == " foo bar"
  25. doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
  26. doAssert expandTabs("ab\tcd\n\txy\t", 3) == "ab cd\n xy "
  27. result = newStringOfCap(s.len + s.len shr 2)
  28. var pos = 0
  29. template addSpaces(n) =
  30. for j in 0 ..< n:
  31. result.add(' ')
  32. pos += 1
  33. for i in 0 ..< len(s):
  34. let c = s[i]
  35. if c == '\t':
  36. let
  37. denominator = if tabSize > 0: tabSize else: 1
  38. numSpaces = tabSize - pos mod denominator
  39. addSpaces(numSpaces)
  40. else:
  41. result.add(c)
  42. pos += 1
  43. if c == '\l':
  44. pos = 0
  45. proc partition*(s: string, sep: string,
  46. right: bool = false): (string, string, string)
  47. {.noSideEffect.} =
  48. ## Split the string at the first or last occurrence of `sep` into a 3-tuple
  49. ##
  50. ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  51. ## (`s`, "", "") if `sep` is not found and `right` is false or
  52. ## ("", "", `s`) if `sep` is not found and `right` is true
  53. runnableExamples:
  54. doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
  55. doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
  56. doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
  57. doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
  58. doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
  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. proc rpartition*(s: string, sep: string): (string, string, string)
  64. {.noSideEffect.} =
  65. ## Split the string at the last occurrence of `sep` into a 3-tuple
  66. ##
  67. ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  68. ## ("", "", `s`) if `sep` is not found
  69. runnableExamples:
  70. doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
  71. doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
  72. doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
  73. doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
  74. doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")
  75. return partition(s, sep, right = true)
  76. when isMainModule:
  77. doAssert expandTabs("\t", 4) == " "
  78. doAssert expandTabs("\tfoo\t", 4) == " foo "
  79. doAssert expandTabs("\tfoo\tbar", 4) == " foo bar"
  80. doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
  81. doAssert expandTabs("", 4) == ""
  82. doAssert expandTabs("", 0) == ""
  83. doAssert expandTabs("\t\t\t", 0) == ""
  84. doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
  85. doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
  86. doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
  87. doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
  88. doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
  89. doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
  90. doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
  91. doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
  92. doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
  93. doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")