strmisc.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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` by `tabSize` spaces
  16. result = newStringOfCap(s.len + s.len shr 2)
  17. var pos = 0
  18. template addSpaces(n) =
  19. for j in 0 ..< n:
  20. result.add(' ')
  21. pos += 1
  22. for i in 0 ..< len(s):
  23. let c = s[i]
  24. if c == '\t':
  25. let
  26. denominator = if tabSize > 0: tabSize else: 1
  27. numSpaces = tabSize - pos mod denominator
  28. addSpaces(numSpaces)
  29. else:
  30. result.add(c)
  31. pos += 1
  32. if c == '\l':
  33. pos = 0
  34. proc partition*(s: string, sep: string,
  35. right: bool = false): (string, string, string)
  36. {.noSideEffect, procvar.} =
  37. ## Split the string at the first or last occurrence of `sep` into a 3-tuple
  38. ##
  39. ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
  40. ## (`s`, "", "") if `sep` is not found and `right` is false or
  41. ## ("", "", `s`) if `sep` is not found and `right` is true
  42. let position = if right: s.rfind(sep) else: s.find(sep)
  43. if position != -1:
  44. return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
  45. return if right: ("", "", s) else: (s, "", "")
  46. proc rpartition*(s: string, sep: string): (string, string, string)
  47. {.noSideEffect, procvar.} =
  48. ## Split the string at the 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
  52. return partition(s, sep, right = true)
  53. when isMainModule:
  54. doAssert expandTabs("\t", 4) == " "
  55. doAssert expandTabs("\tfoo\t", 4) == " foo "
  56. doAssert expandTabs("\tfoo\tbar", 4) == " foo bar"
  57. doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
  58. doAssert expandTabs("", 4) == ""
  59. doAssert expandTabs("", 0) == ""
  60. doAssert expandTabs("\t\t\t", 0) == ""
  61. doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
  62. doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
  63. doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
  64. doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
  65. doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
  66. doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
  67. doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
  68. doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
  69. doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
  70. doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")