Homogenizes docstrings for strutils module.
This mostly involves separating the short description string from the full description block, capitalizing it, and adding any worthwhile hyperlinks to other symbols.
This commit is contained in:
parent
5a3c88d3ad
commit
7f276fa9e0
1 changed files with 156 additions and 94 deletions
|
|
@ -48,9 +48,11 @@ const
|
||||||
## the set of characters a newline terminator can start with
|
## the set of characters a newline terminator can start with
|
||||||
|
|
||||||
AllChars* = {'\x00'..'\xFF'}
|
AllChars* = {'\x00'..'\xFF'}
|
||||||
## A set with all the possible characters. Not very useful by its own, you
|
## A set with all the possible characters.
|
||||||
## can use it to create *inverted* sets to make the ``find()`` proc find
|
##
|
||||||
## **invalid** characters in strings. Example:
|
## Not very useful by its own, you can use it to create *inverted* sets to
|
||||||
|
## make the `find() proc <#find,string,set[char],int>`_ find **invalid**
|
||||||
|
## characters in strings. Example:
|
||||||
##
|
##
|
||||||
## .. code-block:: nimrod
|
## .. code-block:: nimrod
|
||||||
## let invalid = AllChars - Digits
|
## let invalid = AllChars - Digits
|
||||||
|
|
@ -59,8 +61,11 @@ const
|
||||||
|
|
||||||
proc toLower*(c: char): char {.noSideEffect, procvar,
|
proc toLower*(c: char): char {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuToLowerChar".} =
|
rtl, extern: "nsuToLowerChar".} =
|
||||||
## Converts `c` into lower case. This works only for the letters A-Z.
|
## Converts `c` into lower case.
|
||||||
## See `unicode.toLower` for a version that works for any Unicode character.
|
##
|
||||||
|
## This works only for the letters ``A-Z``. See `unicode.toLower
|
||||||
|
## <unicode.html#toLower>`_ for a version that works for any Unicode
|
||||||
|
## character.
|
||||||
if c in {'A'..'Z'}:
|
if c in {'A'..'Z'}:
|
||||||
result = chr(ord(c) + (ord('a') - ord('A')))
|
result = chr(ord(c) + (ord('a') - ord('A')))
|
||||||
else:
|
else:
|
||||||
|
|
@ -68,16 +73,22 @@ proc toLower*(c: char): char {.noSideEffect, procvar,
|
||||||
|
|
||||||
proc toLower*(s: string): string {.noSideEffect, procvar,
|
proc toLower*(s: string): string {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuToLowerStr".} =
|
rtl, extern: "nsuToLowerStr".} =
|
||||||
## Converts `s` into lower case. This works only for the letters A-Z.
|
## Converts `s` into lower case.
|
||||||
## See `unicode.toLower` for a version that works for any Unicode character.
|
##
|
||||||
|
## This works only for the letters ``A-Z``. See `unicode.toLower
|
||||||
|
## <unicode.html#toLower>`_ for a version that works for any Unicode
|
||||||
|
## character.
|
||||||
result = newString(len(s))
|
result = newString(len(s))
|
||||||
for i in 0..len(s) - 1:
|
for i in 0..len(s) - 1:
|
||||||
result[i] = toLower(s[i])
|
result[i] = toLower(s[i])
|
||||||
|
|
||||||
proc toUpper*(c: char): char {.noSideEffect, procvar,
|
proc toUpper*(c: char): char {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuToUpperChar".} =
|
rtl, extern: "nsuToUpperChar".} =
|
||||||
## Converts `c` into upper case. This works only for the letters a-z.
|
## Converts `c` into upper case.
|
||||||
## See `unicode.toUpper` for a version that works for any Unicode character.
|
##
|
||||||
|
## This works only for the letters ``A-Z``. See `unicode.toUpper
|
||||||
|
## <unicode.html#toUpper>`_ for a version that works for any Unicode
|
||||||
|
## character.
|
||||||
if c in {'a'..'z'}:
|
if c in {'a'..'z'}:
|
||||||
result = chr(ord(c) - (ord('a') - ord('A')))
|
result = chr(ord(c) - (ord('a') - ord('A')))
|
||||||
else:
|
else:
|
||||||
|
|
@ -85,8 +96,11 @@ proc toUpper*(c: char): char {.noSideEffect, procvar,
|
||||||
|
|
||||||
proc toUpper*(s: string): string {.noSideEffect, procvar,
|
proc toUpper*(s: string): string {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuToUpperStr".} =
|
rtl, extern: "nsuToUpperStr".} =
|
||||||
## Converts `s` into upper case. This works only for the letters a-z.
|
## Converts `s` into upper case.
|
||||||
## See `unicode.toUpper` for a version that works for any Unicode character.
|
##
|
||||||
|
## This works only for the letters ``A-Z``. See `unicode.toUpper
|
||||||
|
## <unicode.html#toUpper>`_ for a version that works for any Unicode
|
||||||
|
## character.
|
||||||
result = newString(len(s))
|
result = newString(len(s))
|
||||||
for i in 0..len(s) - 1:
|
for i in 0..len(s) - 1:
|
||||||
result[i] = toUpper(s[i])
|
result[i] = toUpper(s[i])
|
||||||
|
|
@ -94,13 +108,16 @@ proc toUpper*(s: string): string {.noSideEffect, procvar,
|
||||||
proc capitalize*(s: string): string {.noSideEffect, procvar,
|
proc capitalize*(s: string): string {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuCapitalize".} =
|
rtl, extern: "nsuCapitalize".} =
|
||||||
## Converts the first character of `s` into upper case.
|
## Converts the first character of `s` into upper case.
|
||||||
## This works only for the letters a-z.
|
##
|
||||||
|
## This works only for the letters ``A-Z``.
|
||||||
result = toUpper(s[0]) & substr(s, 1)
|
result = toUpper(s[0]) & substr(s, 1)
|
||||||
|
|
||||||
proc normalize*(s: string): string {.noSideEffect, procvar,
|
proc normalize*(s: string): string {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuNormalize".} =
|
rtl, extern: "nsuNormalize".} =
|
||||||
## Normalizes the string `s`. That means to convert it to lower case and
|
## Normalizes the string `s`.
|
||||||
## remove any '_'. This is needed for Nimrod identifiers for example.
|
##
|
||||||
|
## That means to convert it to lower case and remove any '_'. This is needed
|
||||||
|
## for Nimrod identifiers for example.
|
||||||
result = newString(s.len)
|
result = newString(s.len)
|
||||||
var j = 0
|
var j = 0
|
||||||
for i in 0..len(s) - 1:
|
for i in 0..len(s) - 1:
|
||||||
|
|
@ -155,6 +172,7 @@ proc cmpIgnoreStyle*(a, b: string): int {.noSideEffect,
|
||||||
proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect,
|
proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect,
|
||||||
rtl, extern: "nsuStrip", operator: 5.} =
|
rtl, extern: "nsuStrip", operator: 5.} =
|
||||||
## Strips whitespace from `s` and returns the resulting string.
|
## Strips whitespace from `s` and returns the resulting string.
|
||||||
|
##
|
||||||
## If `leading` is true, leading whitespace is stripped.
|
## If `leading` is true, leading whitespace is stripped.
|
||||||
## If `trailing` is true, trailing whitespace is stripped.
|
## If `trailing` is true, trailing whitespace is stripped.
|
||||||
const
|
const
|
||||||
|
|
@ -169,8 +187,10 @@ proc strip*(s: string, leading = true, trailing = true): string {.noSideEffect,
|
||||||
result = substr(s, first, last)
|
result = substr(s, first, last)
|
||||||
|
|
||||||
proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} =
|
proc toOctal*(c: char): string {.noSideEffect, rtl, extern: "nsuToOctal".} =
|
||||||
## Converts a character `c` to its octal representation. The resulting
|
## Converts a character `c` to its octal representation.
|
||||||
## string may not have a leading zero. Its length is always exactly 3.
|
##
|
||||||
|
## The resulting string may not have a leading zero. Its length is always
|
||||||
|
## exactly 3.
|
||||||
result = newString(3)
|
result = newString(3)
|
||||||
var val = ord(c)
|
var val = ord(c)
|
||||||
for i in countdown(2, 0):
|
for i in countdown(2, 0):
|
||||||
|
|
@ -348,28 +368,30 @@ proc countLines*(s: string): int {.noSideEffect,
|
||||||
|
|
||||||
proc split*(s: string, seps: set[char] = Whitespace): seq[string] {.
|
proc split*(s: string, seps: set[char] = Whitespace): seq[string] {.
|
||||||
noSideEffect, rtl, extern: "nsuSplitCharSet".} =
|
noSideEffect, rtl, extern: "nsuSplitCharSet".} =
|
||||||
## The same as the `split` iterator, but is a proc that returns a
|
## The same as the `split iterator <#split.i,string,set[char]>`_, but is a
|
||||||
## sequence of substrings.
|
## proc that returns a sequence of substrings.
|
||||||
accumulateResult(split(s, seps))
|
accumulateResult(split(s, seps))
|
||||||
|
|
||||||
proc split*(s: string, sep: char): seq[string] {.noSideEffect,
|
proc split*(s: string, sep: char): seq[string] {.noSideEffect,
|
||||||
rtl, extern: "nsuSplitChar".} =
|
rtl, extern: "nsuSplitChar".} =
|
||||||
## The same as the `split` iterator, but is a proc that returns a sequence
|
## The same as the `split iterator <#split.i,string,char>`_, but is a proc
|
||||||
## of substrings.
|
## that returns a sequence of substrings.
|
||||||
accumulateResult(split(s, sep))
|
accumulateResult(split(s, sep))
|
||||||
|
|
||||||
proc split*(s: string, sep: string): seq[string] {.noSideEffect,
|
proc split*(s: string, sep: string): seq[string] {.noSideEffect,
|
||||||
rtl, extern: "nsuSplitString".} =
|
rtl, extern: "nsuSplitString".} =
|
||||||
## Splits the string `s` into substrings using a string separator.
|
## Splits the string `s` into substrings using a string separator.
|
||||||
##
|
##
|
||||||
## Substrings are separated by the string `sep`.
|
## Substrings are separated by the string `sep`. This is a wrapper around the
|
||||||
|
## `split iterator <#split.i,string,string>`_.
|
||||||
accumulateResult(split(s, sep))
|
accumulateResult(split(s, sep))
|
||||||
|
|
||||||
proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
rtl, extern: "nsuToHex".} =
|
rtl, extern: "nsuToHex".} =
|
||||||
## Converts `x` to its hexadecimal representation. The resulting string
|
## Converts `x` to its hexadecimal representation.
|
||||||
## will be exactly `len` characters long. No prefix like ``0x``
|
##
|
||||||
## is generated. `x` is treated as an unsigned value.
|
## The resulting string will be exactly `len` characters long. No prefix like
|
||||||
|
## ``0x`` is generated. `x` is treated as an unsigned value.
|
||||||
const
|
const
|
||||||
HexChars = "0123456789ABCDEF"
|
HexChars = "0123456789ABCDEF"
|
||||||
var
|
var
|
||||||
|
|
@ -381,9 +403,10 @@ proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
|
|
||||||
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
||||||
rtl, extern: "nsuIntToStr".} =
|
rtl, extern: "nsuIntToStr".} =
|
||||||
## Converts `x` to its decimal representation. The resulting string
|
## Converts `x` to its decimal representation.
|
||||||
## will be minimally `minchars` characters long. This is achieved by
|
##
|
||||||
## adding leading zeros.
|
## The resulting string will be minimally `minchars` characters long. This is
|
||||||
|
## achieved by adding leading zeros.
|
||||||
result = $abs(x)
|
result = $abs(x)
|
||||||
for i in 1 .. minchars - len(result):
|
for i in 1 .. minchars - len(result):
|
||||||
result = '0' & result
|
result = '0' & result
|
||||||
|
|
@ -392,16 +415,18 @@ proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
||||||
|
|
||||||
proc parseInt*(s: string): int {.noSideEffect, procvar,
|
proc parseInt*(s: string): int {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuParseInt".} =
|
rtl, extern: "nsuParseInt".} =
|
||||||
## Parses a decimal integer value contained in `s`. If `s` is not
|
## Parses a decimal integer value contained in `s`.
|
||||||
## a valid integer, `EInvalidValue` is raised.
|
##
|
||||||
|
## If `s` is not a valid integer, `EInvalidValue` is raised.
|
||||||
var L = parseutils.parseInt(s, result, 0)
|
var L = parseutils.parseInt(s, result, 0)
|
||||||
if L != s.len or L == 0:
|
if L != s.len or L == 0:
|
||||||
raise newException(EInvalidValue, "invalid integer: " & s)
|
raise newException(EInvalidValue, "invalid integer: " & s)
|
||||||
|
|
||||||
proc parseBiggestInt*(s: string): BiggestInt {.noSideEffect, procvar,
|
proc parseBiggestInt*(s: string): BiggestInt {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuParseBiggestInt".} =
|
rtl, extern: "nsuParseBiggestInt".} =
|
||||||
## Parses a decimal integer value contained in `s`. If `s` is not
|
## Parses a decimal integer value contained in `s`.
|
||||||
## a valid integer, `EInvalidValue` is raised.
|
##
|
||||||
|
## If `s` is not a valid integer, `EInvalidValue` is raised.
|
||||||
var L = parseutils.parseBiggestInt(s, result, 0)
|
var L = parseutils.parseBiggestInt(s, result, 0)
|
||||||
if L != s.len or L == 0:
|
if L != s.len or L == 0:
|
||||||
raise newException(EInvalidValue, "invalid integer: " & s)
|
raise newException(EInvalidValue, "invalid integer: " & s)
|
||||||
|
|
@ -417,10 +442,11 @@ proc parseFloat*(s: string): float {.noSideEffect, procvar,
|
||||||
|
|
||||||
proc parseHexInt*(s: string): int {.noSideEffect, procvar,
|
proc parseHexInt*(s: string): int {.noSideEffect, procvar,
|
||||||
rtl, extern: "nsuParseHexInt".} =
|
rtl, extern: "nsuParseHexInt".} =
|
||||||
## Parses a hexadecimal integer value contained in `s`. If `s` is not
|
## Parses a hexadecimal integer value contained in `s`.
|
||||||
## a valid integer, `EInvalidValue` is raised. `s` can have one of the
|
##
|
||||||
## following optional prefixes: ``0x``, ``0X``, ``#``.
|
## If `s` is not a valid integer, `EInvalidValue` is raised. `s` can have one
|
||||||
## Underscores within `s` are ignored.
|
## of the following optional prefixes: ``0x``, ``0X``, ``#``. Underscores
|
||||||
|
## within `s` are ignored.
|
||||||
var i = 0
|
var i = 0
|
||||||
if s[i] == '0' and (s[i+1] == 'x' or s[i+1] == 'X'): inc(i, 2)
|
if s[i] == '0' and (s[i+1] == 'x' or s[i+1] == 'X'): inc(i, 2)
|
||||||
elif s[i] == '#': inc(i)
|
elif s[i] == '#': inc(i)
|
||||||
|
|
@ -440,26 +466,32 @@ proc parseHexInt*(s: string): int {.noSideEffect, procvar,
|
||||||
else: raise newException(EInvalidValue, "invalid integer: " & s)
|
else: raise newException(EInvalidValue, "invalid integer: " & s)
|
||||||
|
|
||||||
proc parseBool*(s: string): bool =
|
proc parseBool*(s: string): bool =
|
||||||
## Parses a value into a `bool`. If ``s`` is one of the following values:
|
## Parses a value into a `bool`.
|
||||||
## ``y, yes, true, 1, on``, then returns `true`. If ``s`` is one of the
|
##
|
||||||
## following values: ``n, no, false, 0, off``, then returns `false`.
|
## If ``s`` is one of the following values: ``y, yes, true, 1, on``, then
|
||||||
## If ``s`` is something else a ``EInvalidValue`` exception is raised.
|
## returns `true`. If ``s`` is one of the following values: ``n, no, false,
|
||||||
|
## 0, off``, then returns `false`. If ``s`` is something else a
|
||||||
|
## ``EInvalidValue`` exception is raised.
|
||||||
case normalize(s)
|
case normalize(s)
|
||||||
of "y", "yes", "true", "1", "on": result = true
|
of "y", "yes", "true", "1", "on": result = true
|
||||||
of "n", "no", "false", "0", "off": result = false
|
of "n", "no", "false", "0", "off": result = false
|
||||||
else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s)
|
else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s)
|
||||||
|
|
||||||
proc parseEnum*[T: enum](s: string): T =
|
proc parseEnum*[T: enum](s: string): T =
|
||||||
## parses an enum ``T``. Raises ``EInvalidValue`` for an invalid value in
|
## Parses an enum ``T``.
|
||||||
## `s`. The comparison is done in a style insensitive way.
|
##
|
||||||
|
## Raises ``EInvalidValue`` for an invalid value in `s`. The comparison is
|
||||||
|
## done in a style insensitive way.
|
||||||
for e in low(T)..high(T):
|
for e in low(T)..high(T):
|
||||||
if cmpIgnoreStyle(s, $e) == 0:
|
if cmpIgnoreStyle(s, $e) == 0:
|
||||||
return e
|
return e
|
||||||
raise newException(EInvalidValue, "invalid enum value: " & s)
|
raise newException(EInvalidValue, "invalid enum value: " & s)
|
||||||
|
|
||||||
proc parseEnum*[T: enum](s: string, default: T): T =
|
proc parseEnum*[T: enum](s: string, default: T): T =
|
||||||
## parses an enum ``T``. Uses `default` for an invalid value in
|
## Parses an enum ``T``.
|
||||||
## `s`. The comparison is done in a style insensitive way.
|
##
|
||||||
|
## Uses `default` for an invalid value in `s`. The comparison is done in a
|
||||||
|
## style insensitive way.
|
||||||
for e in low(T)..high(T):
|
for e in low(T)..high(T):
|
||||||
if cmpIgnoreStyle(s, $e) == 0:
|
if cmpIgnoreStyle(s, $e) == 0:
|
||||||
return e
|
return e
|
||||||
|
|
@ -489,10 +521,11 @@ proc repeatStr*(count: int, s: string): string {.noSideEffect,
|
||||||
proc align*(s: string, count: int, padding = ' '): string {.
|
proc align*(s: string, count: int, padding = ' '): string {.
|
||||||
noSideEffect, rtl, extern: "nsuAlignString".} =
|
noSideEffect, rtl, extern: "nsuAlignString".} =
|
||||||
## Aligns a string `s` with `padding`, so that is of length `count`.
|
## Aligns a string `s` with `padding`, so that is of length `count`.
|
||||||
|
##
|
||||||
## `padding` characters (by default spaces) are added before `s` resulting in
|
## `padding` characters (by default spaces) are added before `s` resulting in
|
||||||
## right alignment. If ``s.len >= count``, no spaces are added and `s` is
|
## right alignment. If ``s.len >= count``, no spaces are added and `s` is
|
||||||
## returned unchanged. If you need to left align a string use the
|
## returned unchanged. If you need to left align a string use the `repeatChar
|
||||||
## ``repeatChar`` proc. Example:
|
## proc <#repeatChar>`_. Example:
|
||||||
##
|
##
|
||||||
## .. code-block:: nimrod
|
## .. code-block:: nimrod
|
||||||
## assert align("abc", 4) == " abc"
|
## assert align("abc", 4) == " abc"
|
||||||
|
|
@ -546,7 +579,7 @@ proc wordWrap*(s: string, maxLineWidth = 80,
|
||||||
seps: set[char] = Whitespace,
|
seps: set[char] = Whitespace,
|
||||||
newLine = "\n"): string {.
|
newLine = "\n"): string {.
|
||||||
noSideEffect, rtl, extern: "nsuWordWrap".} =
|
noSideEffect, rtl, extern: "nsuWordWrap".} =
|
||||||
## word wraps `s`.
|
## Word wraps `s`.
|
||||||
result = newStringOfCap(s.len + s.len shr 6)
|
result = newStringOfCap(s.len + s.len shr 6)
|
||||||
var spaceLeft = maxLineWidth
|
var spaceLeft = maxLineWidth
|
||||||
var lastSep = ""
|
var lastSep = ""
|
||||||
|
|
@ -578,7 +611,7 @@ proc wordWrap*(s: string, maxLineWidth = 80,
|
||||||
|
|
||||||
proc unindent*(s: string, eatAllIndent = false): string {.
|
proc unindent*(s: string, eatAllIndent = false): string {.
|
||||||
noSideEffect, rtl, extern: "nsuUnindent".} =
|
noSideEffect, rtl, extern: "nsuUnindent".} =
|
||||||
## unindents `s`.
|
## Unindents `s`.
|
||||||
result = newStringOfCap(s.len)
|
result = newStringOfCap(s.len)
|
||||||
var i = 0
|
var i = 0
|
||||||
var pattern = true
|
var pattern = true
|
||||||
|
|
@ -606,6 +639,7 @@ proc unindent*(s: string, eatAllIndent = false): string {.
|
||||||
proc startsWith*(s, prefix: string): bool {.noSideEffect,
|
proc startsWith*(s, prefix: string): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuStartsWith".} =
|
rtl, extern: "nsuStartsWith".} =
|
||||||
## Returns true iff ``s`` starts with ``prefix``.
|
## Returns true iff ``s`` starts with ``prefix``.
|
||||||
|
##
|
||||||
## If ``prefix == ""`` true is returned.
|
## If ``prefix == ""`` true is returned.
|
||||||
var i = 0
|
var i = 0
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -616,6 +650,7 @@ proc startsWith*(s, prefix: string): bool {.noSideEffect,
|
||||||
proc endsWith*(s, suffix: string): bool {.noSideEffect,
|
proc endsWith*(s, suffix: string): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuEndsWith".} =
|
rtl, extern: "nsuEndsWith".} =
|
||||||
## Returns true iff ``s`` ends with ``suffix``.
|
## Returns true iff ``s`` ends with ``suffix``.
|
||||||
|
##
|
||||||
## If ``suffix == ""`` true is returned.
|
## If ``suffix == ""`` true is returned.
|
||||||
var i = 0
|
var i = 0
|
||||||
var j = len(s) - len(suffix)
|
var j = len(s) - len(suffix)
|
||||||
|
|
@ -627,6 +662,7 @@ proc endsWith*(s, suffix: string): bool {.noSideEffect,
|
||||||
proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect,
|
proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuContinuesWith".} =
|
rtl, extern: "nsuContinuesWith".} =
|
||||||
## Returns true iff ``s`` continues with ``substr`` at position ``start``.
|
## Returns true iff ``s`` continues with ``substr`` at position ``start``.
|
||||||
|
##
|
||||||
## If ``substr == ""`` true is returned.
|
## If ``substr == ""`` true is returned.
|
||||||
var i = 0
|
var i = 0
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -636,6 +672,8 @@ proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect,
|
||||||
|
|
||||||
proc addSep*(dest: var string, sep = ", ", startLen = 0) {.noSideEffect,
|
proc addSep*(dest: var string, sep = ", ", startLen = 0) {.noSideEffect,
|
||||||
inline.} =
|
inline.} =
|
||||||
|
## Adds a separator to `dest` only if its length is bigger than `startLen`.
|
||||||
|
##
|
||||||
## A shorthand for:
|
## A shorthand for:
|
||||||
##
|
##
|
||||||
## .. code-block:: nimrod
|
## .. code-block:: nimrod
|
||||||
|
|
@ -655,15 +693,15 @@ proc addSep*(dest: var string, sep = ", ", startLen = 0) {.noSideEffect,
|
||||||
if dest.len > startLen: add(dest, sep)
|
if dest.len > startLen: add(dest, sep)
|
||||||
|
|
||||||
proc allCharsInSet*(s: string, theSet: TCharSet): bool =
|
proc allCharsInSet*(s: string, theSet: TCharSet): bool =
|
||||||
## returns true iff each character of `s` is in the set `theSet`.
|
## Returns true iff each character of `s` is in the set `theSet`.
|
||||||
for c in items(s):
|
for c in items(s):
|
||||||
if c notin theSet: return false
|
if c notin theSet: return false
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc abbrev*(s: string, possibilities: openArray[string]): int =
|
proc abbrev*(s: string, possibilities: openArray[string]): int =
|
||||||
## returns the index of the first item in `possibilities` if not
|
## Returns the index of the first item in `possibilities` if not ambiguous.
|
||||||
## ambiguous; -1 if no item has been found; -2 if multiple items
|
##
|
||||||
## match.
|
## Returns -1 if no item has been found and -2 if multiple items match.
|
||||||
result = -1 # none found
|
result = -1 # none found
|
||||||
for i in 0..possibilities.len-1:
|
for i in 0..possibilities.len-1:
|
||||||
if possibilities[i].startsWith(s):
|
if possibilities[i].startsWith(s):
|
||||||
|
|
@ -677,7 +715,7 @@ proc abbrev*(s: string, possibilities: openArray[string]): int =
|
||||||
|
|
||||||
proc join*(a: openArray[string], sep: string): string {.
|
proc join*(a: openArray[string], sep: string): string {.
|
||||||
noSideEffect, rtl, extern: "nsuJoinSep".} =
|
noSideEffect, rtl, extern: "nsuJoinSep".} =
|
||||||
## concatenates all strings in `a` separating them with `sep`.
|
## Concatenates all strings in `a` separating them with `sep`.
|
||||||
if len(a) > 0:
|
if len(a) > 0:
|
||||||
var L = sep.len * (a.len-1)
|
var L = sep.len * (a.len-1)
|
||||||
for i in 0..high(a): inc(L, a[i].len)
|
for i in 0..high(a): inc(L, a[i].len)
|
||||||
|
|
@ -691,7 +729,7 @@ proc join*(a: openArray[string], sep: string): string {.
|
||||||
|
|
||||||
proc join*(a: openArray[string]): string {.
|
proc join*(a: openArray[string]): string {.
|
||||||
noSideEffect, rtl, extern: "nsuJoin".} =
|
noSideEffect, rtl, extern: "nsuJoin".} =
|
||||||
## concatenates all strings in `a`.
|
## Concatenates all strings in `a`.
|
||||||
if len(a) > 0:
|
if len(a) > 0:
|
||||||
var L = 0
|
var L = 0
|
||||||
for i in 0..high(a): inc(L, a[i].len)
|
for i in 0..high(a): inc(L, a[i].len)
|
||||||
|
|
@ -709,7 +747,7 @@ proc preprocessSub(sub: string, a: var TSkipTable) =
|
||||||
for i in 0..m-1: a[sub[i]] = m-i
|
for i in 0..m-1: a[sub[i]] = m-i
|
||||||
|
|
||||||
proc findAux(s, sub: string, start: int, a: TSkipTable): int =
|
proc findAux(s, sub: string, start: int, a: TSkipTable): int =
|
||||||
# fast "quick search" algorithm:
|
# Fast "quick search" algorithm:
|
||||||
var
|
var
|
||||||
m = len(sub)
|
m = len(sub)
|
||||||
n = len(s)
|
n = len(s)
|
||||||
|
|
@ -725,32 +763,36 @@ proc findAux(s, sub: string, start: int, a: TSkipTable): int =
|
||||||
|
|
||||||
proc find*(s, sub: string, start: int = 0): int {.noSideEffect,
|
proc find*(s, sub: string, start: int = 0): int {.noSideEffect,
|
||||||
rtl, extern: "nsuFindStr", operator: 6.} =
|
rtl, extern: "nsuFindStr", operator: 6.} =
|
||||||
## Searches for `sub` in `s` starting at position `start`. Searching is
|
## Searches for `sub` in `s` starting at position `start`.
|
||||||
## case-sensitive. If `sub` is not in `s`, -1 is returned.
|
##
|
||||||
|
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
|
||||||
var a {.noinit.}: TSkipTable
|
var a {.noinit.}: TSkipTable
|
||||||
preprocessSub(sub, a)
|
preprocessSub(sub, a)
|
||||||
result = findAux(s, sub, start, a)
|
result = findAux(s, sub, start, a)
|
||||||
|
|
||||||
proc find*(s: string, sub: char, start: int = 0): int {.noSideEffect,
|
proc find*(s: string, sub: char, start: int = 0): int {.noSideEffect,
|
||||||
rtl, extern: "nsuFindChar".} =
|
rtl, extern: "nsuFindChar".} =
|
||||||
## Searches for `sub` in `s` starting at position `start`. Searching is
|
## Searches for `sub` in `s` starting at position `start`.
|
||||||
## case-sensitive. If `sub` is not in `s`, -1 is returned.
|
##
|
||||||
|
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
|
||||||
for i in start..len(s)-1:
|
for i in start..len(s)-1:
|
||||||
if sub == s[i]: return i
|
if sub == s[i]: return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
proc find*(s: string, chars: set[char], start: int = 0): int {.noSideEffect,
|
proc find*(s: string, chars: set[char], start: int = 0): int {.noSideEffect,
|
||||||
rtl, extern: "nsuFindCharSet".} =
|
rtl, extern: "nsuFindCharSet".} =
|
||||||
## Searches for `chars` in `s` starting at position `start`. If `s` contains
|
## Searches for `chars` in `s` starting at position `start`.
|
||||||
## none of the characters in `chars`, -1 is returned.
|
##
|
||||||
|
## If `s` contains none of the characters in `chars`, -1 is returned.
|
||||||
for i in start..s.len-1:
|
for i in start..s.len-1:
|
||||||
if s[i] in chars: return i
|
if s[i] in chars: return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
|
proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
|
||||||
## Searches for `sub` in `s` in reverse, starting at `start` and going
|
## Searches for `sub` in `s` in reverse, starting at `start` and going
|
||||||
## backwards to 0. Searching is case-sensitive. If `sub` is not in `s`, -1 is
|
## backwards to 0.
|
||||||
## returned.
|
##
|
||||||
|
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
|
||||||
let realStart = if start == -1: s.len else: start
|
let realStart = if start == -1: s.len else: start
|
||||||
for i in countdown(realStart-sub.len, 0):
|
for i in countdown(realStart-sub.len, 0):
|
||||||
for j in 0..sub.len-1:
|
for j in 0..sub.len-1:
|
||||||
|
|
@ -762,10 +804,11 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
proc quoteIfContainsWhite*(s: string): string {.deprecated.} =
|
proc quoteIfContainsWhite*(s: string): string {.deprecated.} =
|
||||||
## returns ``'"' & s & '"'`` if `s` contains a space and does not
|
## Returns ``'"' & s & '"'`` if `s` contains a space and does not
|
||||||
## start with a quote, else returns `s`
|
## start with a quote, else returns `s`.
|
||||||
## DEPRECATED as it was confused for shell quoting function.
|
##
|
||||||
## For this application use osproc.quoteShell.
|
## **DEPRECATED** as it was confused for shell quoting function. For this
|
||||||
|
## application use `osproc.quoteShell <osproc.html#quoteShell>`_.
|
||||||
if find(s, {' ', '\t'}) >= 0 and s[0] != '"':
|
if find(s, {' ', '\t'}) >= 0 and s[0] != '"':
|
||||||
result = '"' & s & '"'
|
result = '"' & s & '"'
|
||||||
else:
|
else:
|
||||||
|
|
@ -801,7 +844,9 @@ proc replace*(s, sub: string, by = ""): string {.noSideEffect,
|
||||||
|
|
||||||
proc replace*(s: string, sub, by: char): string {.noSideEffect,
|
proc replace*(s: string, sub, by: char): string {.noSideEffect,
|
||||||
rtl, extern: "nsuReplaceChar".} =
|
rtl, extern: "nsuReplaceChar".} =
|
||||||
## optimized version for characters.
|
## Replaces `sub` in `s` by the character `by`.
|
||||||
|
##
|
||||||
|
## Optimized version of `replace <#replace,string,string>`_ for characters.
|
||||||
result = newString(s.len)
|
result = newString(s.len)
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < s.len:
|
while i < s.len:
|
||||||
|
|
@ -811,9 +856,11 @@ proc replace*(s: string, sub, by: char): string {.noSideEffect,
|
||||||
|
|
||||||
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`. Each occurance of `sub`
|
## Replaces `sub` in `s` by the string `by`.
|
||||||
## has to be surrounded by word boundaries (comparable to ``\\w`` in
|
##
|
||||||
## regular expressions), otherwise it is not replaced.
|
## Each occurance of `sub` has to be surrounded by word boundaries
|
||||||
|
## (comparable to ``\\w`` in regular expressions), otherwise it is not
|
||||||
|
## replaced.
|
||||||
const wordChars = {'a'..'z', 'A'..'Z', '0'..'9', '_', '\128'..'\255'}
|
const wordChars = {'a'..'z', 'A'..'Z', '0'..'9', '_', '\128'..'\255'}
|
||||||
var a {.noinit.}: TSkipTable
|
var a {.noinit.}: TSkipTable
|
||||||
result = ""
|
result = ""
|
||||||
|
|
@ -836,8 +883,9 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
|
||||||
|
|
||||||
proc delete*(s: var string, first, last: int) {.noSideEffect,
|
proc delete*(s: var string, first, last: int) {.noSideEffect,
|
||||||
rtl, extern: "nsuDelete".} =
|
rtl, extern: "nsuDelete".} =
|
||||||
## Deletes in `s` the characters at position `first` .. `last`. This modifies
|
## Deletes in `s` the characters at position `first` .. `last`.
|
||||||
## `s` itself, it does not return a copy.
|
##
|
||||||
|
## This modifies `s` itself, it does not return a copy.
|
||||||
var i = first
|
var i = first
|
||||||
var j = last+1
|
var j = last+1
|
||||||
var newLen = len(s)-j+i
|
var newLen = len(s)-j+i
|
||||||
|
|
@ -849,10 +897,11 @@ proc delete*(s: var string, first, last: int) {.noSideEffect,
|
||||||
|
|
||||||
proc parseOctInt*(s: string): int {.noSideEffect,
|
proc parseOctInt*(s: string): int {.noSideEffect,
|
||||||
rtl, extern: "nsuParseOctInt".} =
|
rtl, extern: "nsuParseOctInt".} =
|
||||||
## Parses an octal integer value contained in `s`. If `s` is not
|
## Parses an octal integer value contained in `s`.
|
||||||
## a valid integer, `EInvalidValue` is raised. `s` can have one of the
|
##
|
||||||
## following optional prefixes: ``0o``, ``0O``.
|
## If `s` is not a valid integer, `EInvalidValue` is raised. `s` can have one
|
||||||
## Underscores within `s` are ignored.
|
## of the following optional prefixes: ``0o``, ``0O``. Underscores within
|
||||||
|
## `s` are ignored.
|
||||||
var i = 0
|
var i = 0
|
||||||
if s[i] == '0' and (s[i+1] == 'o' or s[i+1] == 'O'): inc(i, 2)
|
if s[i] == '0' and (s[i+1] == 'o' or s[i+1] == 'O'): inc(i, 2)
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -866,8 +915,10 @@ proc parseOctInt*(s: string): int {.noSideEffect,
|
||||||
|
|
||||||
proc toOct*(x: BiggestInt, len: int): string {.noSideEffect,
|
proc toOct*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
rtl, extern: "nsuToOct".} =
|
rtl, extern: "nsuToOct".} =
|
||||||
## converts `x` into its octal representation. The resulting string is
|
## Converts `x` into its octal representation.
|
||||||
## always `len` characters long. No leading ``0o`` prefix is generated.
|
##
|
||||||
|
## The resulting string is always `len` characters long. No leading ``0o``
|
||||||
|
## prefix is generated.
|
||||||
var
|
var
|
||||||
mask: BiggestInt = 7
|
mask: BiggestInt = 7
|
||||||
shift: BiggestInt = 0
|
shift: BiggestInt = 0
|
||||||
|
|
@ -880,8 +931,10 @@ proc toOct*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
|
|
||||||
proc toBin*(x: BiggestInt, len: int): string {.noSideEffect,
|
proc toBin*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
rtl, extern: "nsuToBin".} =
|
rtl, extern: "nsuToBin".} =
|
||||||
## converts `x` into its binary representation. The resulting string is
|
## Converts `x` into its binary representation.
|
||||||
## always `len` characters long. No leading ``0b`` prefix is generated.
|
##
|
||||||
|
## The resulting string is always `len` characters long. No leading ``0b``
|
||||||
|
## prefix is generated.
|
||||||
var
|
var
|
||||||
mask: BiggestInt = 1
|
mask: BiggestInt = 1
|
||||||
shift: BiggestInt = 0
|
shift: BiggestInt = 0
|
||||||
|
|
@ -894,7 +947,8 @@ proc toBin*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
|
|
||||||
proc insertSep*(s: string, sep = '_', digits = 3): string {.noSideEffect,
|
proc insertSep*(s: string, sep = '_', digits = 3): string {.noSideEffect,
|
||||||
rtl, extern: "nsuInsertSep".} =
|
rtl, extern: "nsuInsertSep".} =
|
||||||
## inserts the separator `sep` after `digits` digits from right to left.
|
## Inserts the separator `sep` after `digits` digits from right to left.
|
||||||
|
##
|
||||||
## Even though the algorithm works with any string `s`, it is only useful
|
## Even though the algorithm works with any string `s`, it is only useful
|
||||||
## if `s` contains a number.
|
## if `s` contains a number.
|
||||||
## Example: ``insertSep("1000000") == "1_000_000"``
|
## Example: ``insertSep("1000000") == "1_000_000"``
|
||||||
|
|
@ -913,7 +967,9 @@ proc insertSep*(s: string, sep = '_', digits = 3): string {.noSideEffect,
|
||||||
|
|
||||||
proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
rtl, extern: "nsuEscape".} =
|
rtl, extern: "nsuEscape".} =
|
||||||
## Escapes a string `s`. This does these operations (at the same time):
|
## Escapes a string `s`.
|
||||||
|
##
|
||||||
|
## This does these operations (at the same time):
|
||||||
## * replaces any ``\`` by ``\\``
|
## * replaces any ``\`` by ``\\``
|
||||||
## * replaces any ``'`` by ``\'``
|
## * replaces any ``'`` by ``\'``
|
||||||
## * replaces any ``"`` by ``\"``
|
## * replaces any ``"`` by ``\"``
|
||||||
|
|
@ -937,11 +993,13 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
|
|
||||||
proc unescape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
proc unescape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
rtl, extern: "nsuUnescape".} =
|
rtl, extern: "nsuUnescape".} =
|
||||||
## Unescapes a string `s`. This complements ``escape`` as it performs the
|
## Unescapes a string `s`.
|
||||||
## opposite operations.
|
|
||||||
##
|
##
|
||||||
## If `s` does not begin with ``prefix`` and end with ``suffix`` a EInvalidValue
|
## This complements `escape <#escape>`_ as it performs the opposite
|
||||||
## exception will be raised.
|
## operations.
|
||||||
|
##
|
||||||
|
## If `s` does not begin with ``prefix`` and end with ``suffix`` a
|
||||||
|
## EInvalidValue exception will be raised.
|
||||||
result = newStringOfCap(s.len)
|
result = newStringOfCap(s.len)
|
||||||
var i = 0
|
var i = 0
|
||||||
if s[0 .. prefix.len-1] != prefix:
|
if s[0 .. prefix.len-1] != prefix:
|
||||||
|
|
@ -975,9 +1033,10 @@ proc unescape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
|
||||||
|
|
||||||
proc validIdentifier*(s: string): bool {.noSideEffect,
|
proc validIdentifier*(s: string): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuValidIdentifier".} =
|
rtl, extern: "nsuValidIdentifier".} =
|
||||||
## returns true if `s` is a valid identifier. A valid identifier starts
|
## Returns true if `s` is a valid identifier.
|
||||||
## with a character of the set `IdentStartChars` and is followed by any
|
##
|
||||||
## number of characters of the set `IdentChars`.
|
## A valid identifier starts with a character of the set `IdentStartChars`
|
||||||
|
## and is followed by any number of characters of the set `IdentChars`.
|
||||||
if s[0] in IdentStartChars:
|
if s[0] in IdentStartChars:
|
||||||
for i in 1..s.len-1:
|
for i in 1..s.len-1:
|
||||||
if s[i] notin IdentChars: return false
|
if s[i] notin IdentChars: return false
|
||||||
|
|
@ -985,9 +1044,10 @@ proc validIdentifier*(s: string): bool {.noSideEffect,
|
||||||
|
|
||||||
proc editDistance*(a, b: string): int {.noSideEffect,
|
proc editDistance*(a, b: string): int {.noSideEffect,
|
||||||
rtl, extern: "nsuEditDistance".} =
|
rtl, extern: "nsuEditDistance".} =
|
||||||
## returns the edit distance between `a` and `b`. This uses the
|
## Returns the edit distance between `a` and `b`.
|
||||||
## `Levenshtein`:idx: distance algorithm with only a linear memory overhead.
|
##
|
||||||
## This implementation is highly optimized!
|
## This uses the `Levenshtein`:idx: distance algorithm with only a linear
|
||||||
|
## memory overhead. This implementation is highly optimized!
|
||||||
var len1 = a.len
|
var len1 = a.len
|
||||||
var len2 = b.len
|
var len2 = b.len
|
||||||
if len1 > len2:
|
if len1 > len2:
|
||||||
|
|
@ -1086,7 +1146,7 @@ type
|
||||||
proc formatBiggestFloat*(f: BiggestFloat, format: TFloatFormat = ffDefault,
|
proc formatBiggestFloat*(f: BiggestFloat, format: TFloatFormat = ffDefault,
|
||||||
precision: range[0..32] = 16): string {.
|
precision: range[0..32] = 16): string {.
|
||||||
noSideEffect, operator: 2, rtl, extern: "nsu$1".} =
|
noSideEffect, operator: 2, rtl, extern: "nsu$1".} =
|
||||||
## converts a floating point value `f` to a string.
|
## Converts a floating point value `f` to a string.
|
||||||
##
|
##
|
||||||
## If ``format == ffDecimal`` then precision is the number of digits to
|
## If ``format == ffDecimal`` then precision is the number of digits to
|
||||||
## be printed after the decimal point.
|
## be printed after the decimal point.
|
||||||
|
|
@ -1117,7 +1177,7 @@ proc formatBiggestFloat*(f: BiggestFloat, format: TFloatFormat = ffDefault,
|
||||||
proc formatFloat*(f: float, format: TFloatFormat = ffDefault,
|
proc formatFloat*(f: float, format: TFloatFormat = ffDefault,
|
||||||
precision: range[0..32] = 16): string {.
|
precision: range[0..32] = 16): string {.
|
||||||
noSideEffect, operator: 2, rtl, extern: "nsu$1".} =
|
noSideEffect, operator: 2, rtl, extern: "nsu$1".} =
|
||||||
## converts a floating point value `f` to a string.
|
## Converts a floating point value `f` to a string.
|
||||||
##
|
##
|
||||||
## If ``format == ffDecimal`` then precision is the number of digits to
|
## If ``format == ffDecimal`` then precision is the number of digits to
|
||||||
## be printed after the decimal point.
|
## be printed after the decimal point.
|
||||||
|
|
@ -1212,6 +1272,8 @@ proc addf*(s: var string, formatstr: string, a: varargs[string, `$`]) {.
|
||||||
|
|
||||||
proc `%` *(formatstr: string, a: openArray[string]): string {.noSideEffect,
|
proc `%` *(formatstr: string, a: openArray[string]): string {.noSideEffect,
|
||||||
rtl, extern: "nsuFormatOpenArray".} =
|
rtl, extern: "nsuFormatOpenArray".} =
|
||||||
|
## Interpolates a format string with the values from `a`.
|
||||||
|
##
|
||||||
## The `substitution`:idx: operator performs string substitutions in
|
## The `substitution`:idx: operator performs string substitutions in
|
||||||
## `formatstr` and returns a modified `formatstr`. This is often called
|
## `formatstr` and returns a modified `formatstr`. This is often called
|
||||||
## `string interpolation`:idx:.
|
## `string interpolation`:idx:.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue