strmisc.nim 2.9 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 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. template addSpaces(n) =
  27. for _ in 1..n:
  28. result.add(' ')
  29. pos += n
  30. var pos = 0
  31. let denominator = if tabSize > 0: tabSize else: 1
  32. for c in s:
  33. if c == '\t':
  34. let numSpaces = tabSize - pos mod denominator
  35. addSpaces(numSpaces)
  36. else:
  37. result.add(c)
  38. pos += 1
  39. if c == '\l':
  40. pos = 0
  41. func partition*(s: string, sep: string,
  42. right: bool = false): (string, string, string) =
  43. ## Splits the string at the first (if `right` is false)
  44. ## or last (if `right` is true) occurrence of `sep` into a 3-tuple.
  45. ##
  46. ## Returns a 3-tuple of strings, `(beforeSep, sep, afterSep)` or
  47. ## `(s, "", "")` if `sep` is not found and `right` is false or
  48. ## `("", "", s)` if `sep` is not found and `right` is true.
  49. ##
  50. ## **See also:**
  51. ## * `rpartition proc <#rpartition,string,string>`_
  52. runnableExamples:
  53. doAssert partition("foo:bar:baz", ":") == ("foo", ":", "bar:baz")
  54. doAssert partition("foo:bar:baz", ":", right = true) == ("foo:bar", ":", "baz")
  55. doAssert partition("foobar", ":") == ("foobar", "", "")
  56. doAssert partition("foobar", ":", right = true) == ("", "", "foobar")
  57. let position = if right: s.rfind(sep) else: s.find(sep)
  58. if position != -1:
  59. return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
  60. return if right: ("", "", s) else: (s, "", "")
  61. func rpartition*(s: string, sep: string): (string, string, string) =
  62. ## Splits the string at the last occurrence of `sep` into a 3-tuple.
  63. ##
  64. ## Returns a 3-tuple of strings, `(beforeSep, sep, afterSep)` or
  65. ## `("", "", s)` if `sep` is not found. This is the same as
  66. ## `partition(s, sep, right = true)`.
  67. ##
  68. ## **See also:**
  69. ## * `partition proc <#partition,string,string,bool>`_
  70. runnableExamples:
  71. doAssert rpartition("foo:bar:baz", ":") == ("foo:bar", ":", "baz")
  72. doAssert rpartition("foobar", ":") == ("", "", "foobar")
  73. partition(s, sep, right = true)