strmisc.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. {.deadCodeElim: on.} # dce option deprecated
  13. proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect,
  14. procvar.} =
  15. ## Expand tab characters in `s` replacing them by spaces.
  16. ##
  17. ## The amount of inserted spaces for each tab character is the difference
  18. ## between the current column number and the next tab position. Tab positions
  19. ## occur every `tabSize` characters.
  20. ## The column number starts at 0 and is increased with every single character
  21. ## and inserted space, except for newline, which resets the column number
  22. ## back to 0.
  23. runnableExamples:
  24. doAssert expandTabs("\t", 4) == " "
  25. doAssert expandTabs("\tfoo\t", 4) == " foo "
  26. doAssert expandTabs("\tfoo\tbar", 4) == " foo bar"
  27. doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
  28. doAssert expandTabs("ab\tcd\n\txy\t", 3) == "ab cd\n xy "
  29. result = newStringOfCap(s.len + s.len shr 2)
  30. var pos = 0
  31. template addSpaces(n) =
  32. for j in 0 ..< n:
  33. result.add(' ')
  34. pos += 1
  35. for i in 0 ..< len(s):
  36. let c = s[i]
  37. if c == '\t':
  38. let
  39. denominator = if tabSize > 0: tabSize else: 1
  40. numSpaces = tabSize - pos mod denominator
  41. addSpaces(numSpaces)
  42. else:
  43. result.add(c)
  44. pos += 1
  45. if c == '\l':
  46. pos = 0
  47. proc partition*(s: string, sep: string,
  48. right: bool = false): (string, string, string)
  49. {.noSideEffect, procvar.} =
  50. ## Split the string at the first or last occurrence of `sep` into a 3-tuple
  51. ##
  52. ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  53. ## (`s`, "", "") if `sep` is not found and `right` is false or
  54. ## ("", "", `s`) if `sep` is not found and `right` is true
  55. runnableExamples:
  56. doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
  57. doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
  58. doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
  59. doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
  60. doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
  61. let position = if right: s.rfind(sep) else: s.find(sep)
  62. if position != -1:
  63. return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
  64. return if right: ("", "", s) else: (s, "", "")
  65. proc rpartition*(s: string, sep: string): (string, string, string)
  66. {.noSideEffect, procvar.} =
  67. ## Split the string at the last occurrence of `sep` into a 3-tuple
  68. ##
  69. ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  70. ## ("", "", `s`) if `sep` is not found
  71. runnableExamples:
  72. doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
  73. doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
  74. doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
  75. doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
  76. doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")
  77. return partition(s, sep, right = true)
  78. when isMainModule:
  79. doAssert expandTabs("\t", 4) == " "
  80. doAssert expandTabs("\tfoo\t", 4) == " foo "
  81. doAssert expandTabs("\tfoo\tbar", 4) == " foo bar"
  82. doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
  83. doAssert expandTabs("", 4) == ""
  84. doAssert expandTabs("", 0) == ""
  85. doAssert expandTabs("\t\t\t", 0) == ""
  86. doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
  87. doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
  88. doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
  89. doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
  90. doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
  91. doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
  92. doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
  93. doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
  94. doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
  95. doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")