Skip to content

Commit

Permalink
move strmisc to strutils
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Jan 9, 2019
1 parent 88b014e commit 65c2434
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 115 deletions.
4 changes: 0 additions & 4 deletions doc/lib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ String handling
Macro based standard string interpolation / formatting. Inpired by
Python's ```f``-strings.

* `strmisc <strmisc.html>`_
This module contains uncommon string handling operations that do not
fit with the commonly used operations in strutils.

* `parseutils <parseutils.html>`_
This module contains helpers for parsing tokens, numbers, identifiers, etc.

Expand Down
110 changes: 0 additions & 110 deletions lib/pure/strmisc.nim

This file was deleted.

94 changes: 94 additions & 0 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,80 @@ proc stripLineEnd*(s: var string) =
else:
discard

proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect,
procvar.} =
## Expand tab characters in `s` replacing them by spaces.
##
## The amount of inserted spaces for each tab character is the difference
## between the current column number and the next tab position. Tab positions
## occur every `tabSize` characters.
## The column number starts at 0 and is increased with every single character
## and inserted space, except for newline, which resets the column number
## back to 0.
runnableExamples:
doAssert expandTabs("\t", 4) == " "
doAssert expandTabs("\tfoo\t", 4) == " foo "
doAssert expandTabs("\tfoo\tbar", 4) == " foo bar"
doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
doAssert expandTabs("ab\tcd\n\txy\t", 3) == "ab cd\n xy "

result = newStringOfCap(s.len + s.len shr 2)
var pos = 0

template addSpaces(n) =
for j in 0 ..< n:
result.add(' ')
pos += 1

for i in 0 ..< len(s):
let c = s[i]
if c == '\t':
let
denominator = if tabSize > 0: tabSize else: 1
numSpaces = tabSize - pos mod denominator

addSpaces(numSpaces)
else:
result.add(c)
pos += 1
if c == '\l':
pos = 0

proc partition*(s: string, sep: string,
right: bool = false): (string, string, string)
{.noSideEffect, procvar.} =
## Split the string at the first or last occurrence of `sep` into a 3-tuple
##
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
## (`s`, "", "") if `sep` is not found and `right` is false or
## ("", "", `s`) if `sep` is not found and `right` is true
runnableExamples:
doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")

let position = if right: s.rfind(sep) else: s.find(sep)
if position != -1:
return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
return if right: ("", "", s) else: (s, "", "")

proc rpartition*(s: string, sep: string): (string, string, string)
{.noSideEffect, procvar.} =
## Split the string at the last occurrence of `sep` into a 3-tuple
##
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
## ("", "", `s`) if `sep` is not found
runnableExamples:
doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")

return partition(s, sep, right = true)

when isMainModule:
proc nonStaticTests =
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
Expand Down Expand Up @@ -2737,6 +2811,26 @@ bar
doAssert s.splitWhitespace(maxsplit=3) == @["this", "is", "an", "example "]
doAssert s.splitWhitespace(maxsplit=4) == @["this", "is", "an", "example"]

doAssert expandTabs("\t", 4) == " "
doAssert expandTabs("\tfoo\t", 4) == " foo "
doAssert expandTabs("\tfoo\tbar", 4) == " foo bar"
doAssert expandTabs("\tfoo\tbar\t", 4) == " foo bar "
doAssert expandTabs("", 4) == ""
doAssert expandTabs("", 0) == ""
doAssert expandTabs("\t\t\t", 0) == ""

doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")

doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")

block: # startsWith / endsWith char tests
var s = "abcdef"
doAssert s.startsWith('a')
Expand Down
1 change: 0 additions & 1 deletion tools/kochdocs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ lib/pure/streams.nim
lib/pure/terminal.nim
lib/pure/cgi.nim
lib/pure/unicode.nim
lib/pure/strmisc.nim
lib/pure/htmlgen.nim
lib/pure/parseutils.nim
lib/pure/browsers.nim
Expand Down

0 comments on commit 65c2434

Please sign in to comment.