moved expandTabs to strmisc
This commit is contained in:
parent
9950bc3936
commit
42251f0b6b
3 changed files with 38 additions and 50 deletions
|
|
@ -14,6 +14,32 @@ import strutils
|
||||||
|
|
||||||
{.deadCodeElim: on.}
|
{.deadCodeElim: on.}
|
||||||
|
|
||||||
|
proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect,
|
||||||
|
procvar.} =
|
||||||
|
## Expand tab characters in `s` by `tabSize` spaces
|
||||||
|
|
||||||
|
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,
|
proc partition*(s: string, sep: string,
|
||||||
right: bool = false): (string, string, string)
|
right: bool = false): (string, string, string)
|
||||||
{.noSideEffect, procvar.} =
|
{.noSideEffect, procvar.} =
|
||||||
|
|
@ -22,16 +48,9 @@ proc partition*(s: string, sep: string,
|
||||||
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
|
## 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 false or
|
||||||
## ("", "", `s`) if `sep` is not found and `right` is true
|
## ("", "", `s`) if `sep` is not found and `right` is true
|
||||||
|
|
||||||
let position = if right: s.rfind(sep) else: s.find(sep)
|
let position = if right: s.rfind(sep) else: s.find(sep)
|
||||||
|
|
||||||
if position != -1:
|
if position != -1:
|
||||||
let
|
return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
|
||||||
beforeSep = s[0 ..< position]
|
|
||||||
afterSep = s[position + sep.len ..< s.len]
|
|
||||||
|
|
||||||
return (beforeSep, sep, afterSep)
|
|
||||||
|
|
||||||
return if right: ("", "", s) else: (s, "", "")
|
return if right: ("", "", s) else: (s, "", "")
|
||||||
|
|
||||||
proc rpartition*(s: string, sep: string): (string, string, string)
|
proc rpartition*(s: string, sep: string): (string, string, string)
|
||||||
|
|
@ -43,6 +62,14 @@ proc rpartition*(s: string, sep: string): (string, string, string)
|
||||||
return partition(s, sep, right = true)
|
return partition(s, sep, right = true)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
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("foo:bar", ":") == ("foo", ":", "bar")
|
||||||
doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
|
doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
|
||||||
doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
|
doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
|
||||||
|
|
|
||||||
|
|
@ -1319,38 +1319,6 @@ proc replace*(s: string, sub, by: char): string {.noSideEffect,
|
||||||
else: result[i] = s[i]
|
else: result[i] = s[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
|
|
||||||
proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect,
|
|
||||||
procvar, rtl, extern: "nsuExpandTabsStr".} =
|
|
||||||
## Expand tab characters in `s` by `tabSize` spaces
|
|
||||||
|
|
||||||
if len(s) == 0:
|
|
||||||
return s
|
|
||||||
|
|
||||||
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 replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
|
proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
|
||||||
rtl, extern: "nsuReplaceWord".} =
|
rtl, extern: "nsuReplaceWord".} =
|
||||||
## Replaces `sub` in `s` by the string `by`.
|
## Replaces `sub` in `s` by the string `by`.
|
||||||
|
|
@ -2207,14 +2175,6 @@ when isMainModule:
|
||||||
doAssert(not isUpper("AAcc"))
|
doAssert(not isUpper("AAcc"))
|
||||||
doAssert(not isUpper("A#$"))
|
doAssert(not isUpper("A#$"))
|
||||||
|
|
||||||
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 rsplit("foo bar", seps=Whitespace) == @["foo", "bar"]
|
doAssert rsplit("foo bar", seps=Whitespace) == @["foo", "bar"]
|
||||||
doAssert rsplit(" foo bar", seps=Whitespace, maxsplit=1) == @[" foo", "bar"]
|
doAssert rsplit(" foo bar", seps=Whitespace, maxsplit=1) == @[" foo", "bar"]
|
||||||
doAssert rsplit(" foo bar ", seps=Whitespace, maxsplit=1) == @[" foo bar", ""]
|
doAssert rsplit(" foo bar ", seps=Whitespace, maxsplit=1) == @[" foo bar", ""]
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,15 @@ Library Additions
|
||||||
a lightweight alternative to python's ``csv.DictReader``.
|
a lightweight alternative to python's ``csv.DictReader``.
|
||||||
- Added ``setStdIoUnbuffered`` proc to ``system.nim`` to enable unbuffered I/O.
|
- Added ``setStdIoUnbuffered`` proc to ``system.nim`` to enable unbuffered I/O.
|
||||||
|
|
||||||
- Added ``center``, ``rsplit``, and ``expandTabs`` to ``strutils.nim`` to
|
- Added ``center`` and ``rsplit`` to ``strutils.nim`` to
|
||||||
provide similar Python functionality for Nim's strings.
|
provide similar Python functionality for Nim's strings.
|
||||||
|
|
||||||
- Added ``isTitle``, ``title``, and ``swapCase`` to ``unicode.nim`` to
|
- Added ``isTitle``, ``title``, and ``swapCase`` to ``unicode.nim`` to
|
||||||
provide unicode aware string case manipulation.
|
provide unicode aware string case manipulation.
|
||||||
|
|
||||||
- Added a new module ``lib/pure/strmisc.nim`` to hold uncommon string
|
- Added a new module ``lib/pure/strmisc.nim`` to hold uncommon string
|
||||||
operations. Currently contains ``partition`` and ``rpartition``.
|
operations. Currently contains ``partition``, ``rpartition``
|
||||||
|
and ``expandTabs``.
|
||||||
|
|
||||||
Compiler Additions
|
Compiler Additions
|
||||||
------------------
|
------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue