fixes system.runnableExamples; strutils makes use of runnableExamples
This commit is contained in:
parent
8d1a5dc8e7
commit
a720539f5e
2 changed files with 68 additions and 77 deletions
|
|
@ -1854,8 +1854,8 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
||||||
if c.runnableExamples == nil:
|
if c.runnableExamples == nil:
|
||||||
c.runnableExamples = newTree(nkStmtList,
|
c.runnableExamples = newTree(nkStmtList,
|
||||||
newTree(nkImportStmt, newStrNode(nkStrLit, expandFilename(inp))))
|
newTree(nkImportStmt, newStrNode(nkStrLit, expandFilename(inp))))
|
||||||
c.runnableExamples.add newTree(nkBlockStmt, emptyNode, n.lastSon)
|
c.runnableExamples.add newTree(nkBlockStmt, emptyNode, copyTree n.lastSon)
|
||||||
result = n
|
result = setMs(n, s)
|
||||||
else:
|
else:
|
||||||
result = emptyNode
|
result = emptyNode
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -1305,14 +1305,13 @@ proc addSep*(dest: var string, sep = ", ", startLen: Natural = 0)
|
||||||
## This is often useful for generating some code where the items need to
|
## This is often useful for generating some code where the items need to
|
||||||
## be *separated* by `sep`. `sep` is only added if `dest` is longer than
|
## be *separated* by `sep`. `sep` is only added if `dest` is longer than
|
||||||
## `startLen`. The following example creates a string describing
|
## `startLen`. The following example creates a string describing
|
||||||
## an array of integers:
|
## an array of integers.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: nim
|
var arr = "["
|
||||||
## var arr = "["
|
for x in items([2, 3, 5, 7, 11]):
|
||||||
## for x in items([2, 3, 5, 7, 11]):
|
addSep(arr, startLen=len("["))
|
||||||
## addSep(arr, startLen=len("["))
|
add(arr, $x)
|
||||||
## add(arr, $x)
|
add(arr, "]")
|
||||||
## add(arr, "]")
|
|
||||||
if dest.len > startLen: add(dest, sep)
|
if dest.len > startLen: add(dest, sep)
|
||||||
|
|
||||||
proc allCharsInSet*(s: string, theSet: set[char]): bool =
|
proc allCharsInSet*(s: string, theSet: set[char]): bool =
|
||||||
|
|
@ -1730,7 +1729,9 @@ proc insertSep*(s: string, sep = '_', digits = 3): string {.noSideEffect,
|
||||||
##
|
##
|
||||||
## 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"``
|
runnableExamples:
|
||||||
|
doAssert insertSep("1000000") == "1_000_000"
|
||||||
|
|
||||||
var L = (s.len-1) div digits + s.len
|
var L = (s.len-1) div digits + s.len
|
||||||
result = newString(L)
|
result = newString(L)
|
||||||
var j = 0
|
var j = 0
|
||||||
|
|
@ -1818,6 +1819,8 @@ proc validIdentifier*(s: string): bool {.noSideEffect,
|
||||||
##
|
##
|
||||||
## A valid identifier starts with a character of the set `IdentStartChars`
|
## A valid identifier starts with a character of the set `IdentStartChars`
|
||||||
## and is followed by any number of characters of the set `IdentChars`.
|
## and is followed by any number of characters of the set `IdentChars`.
|
||||||
|
runnableExamples:
|
||||||
|
doAssert "abc_def08".validIdentifier
|
||||||
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
|
||||||
|
|
@ -1828,7 +1831,7 @@ proc editDistance*(a, b: string): int {.noSideEffect,
|
||||||
## Returns the edit distance between `a` and `b`.
|
## Returns the edit distance between `a` and `b`.
|
||||||
##
|
##
|
||||||
## This uses the `Levenshtein`:idx: distance algorithm with only a linear
|
## This uses the `Levenshtein`:idx: distance algorithm with only a linear
|
||||||
## memory overhead. This implementation is highly optimized!
|
## memory overhead.
|
||||||
var len1 = a.len
|
var len1 = a.len
|
||||||
var len2 = b.len
|
var len2 = b.len
|
||||||
if len1 > len2:
|
if len1 > len2:
|
||||||
|
|
@ -2007,16 +2010,11 @@ proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
|
||||||
## after the decimal point for Nim's ``float`` type.
|
## after the decimal point for Nim's ``float`` type.
|
||||||
##
|
##
|
||||||
## If ``precision == -1``, it tries to format it nicely.
|
## If ``precision == -1``, it tries to format it nicely.
|
||||||
##
|
runnableExamples:
|
||||||
## Examples:
|
let x = 123.456
|
||||||
##
|
doAssert x.formatFloat() == "123.4560000000000"
|
||||||
## .. code-block:: nim
|
doAssert x.formatFloat(ffDecimal, 4) == "123.4560"
|
||||||
##
|
doAssert x.formatFloat(ffScientific, 2) == "1.23e+02"
|
||||||
## let x = 123.456
|
|
||||||
## doAssert x.formatFloat() == "123.4560000000000"
|
|
||||||
## doAssert x.formatFloat(ffDecimal, 4) == "123.4560"
|
|
||||||
## doAssert x.formatFloat(ffScientific, 2) == "1.23e+02"
|
|
||||||
##
|
|
||||||
result = formatBiggestFloat(f, format, precision, decimalSep)
|
result = formatBiggestFloat(f, format, precision, decimalSep)
|
||||||
|
|
||||||
proc trimZeros*(x: var string) {.noSideEffect.} =
|
proc trimZeros*(x: var string) {.noSideEffect.} =
|
||||||
|
|
@ -2051,18 +2049,13 @@ proc formatSize*(bytes: int64,
|
||||||
##
|
##
|
||||||
## `includeSpace` can be set to true to include the (SI preferred) space
|
## `includeSpace` can be set to true to include the (SI preferred) space
|
||||||
## between the number and the unit (e.g. 1 KiB).
|
## between the number and the unit (e.g. 1 KiB).
|
||||||
##
|
runnableExamples:
|
||||||
## Examples:
|
doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB"
|
||||||
##
|
doAssert formatSize((2.234*1024*1024).int) == "2.234MiB"
|
||||||
## .. code-block:: nim
|
doAssert formatSize(4096, includeSpace=true) == "4 KiB"
|
||||||
##
|
doAssert formatSize(4096, prefix=bpColloquial, includeSpace=true) == "4 kB"
|
||||||
## formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB"
|
doAssert formatSize(4096) == "4KiB"
|
||||||
## formatSize((2.234*1024*1024).int) == "2.234MiB"
|
doAssert formatSize(5_378_934, prefix=bpColloquial, decimalSep=',') == "5,13MB"
|
||||||
## formatSize(4096, includeSpace=true) == "4 KiB"
|
|
||||||
## formatSize(4096, prefix=bpColloquial, includeSpace=true) == "4 kB"
|
|
||||||
## formatSize(4096) == "4KiB"
|
|
||||||
## formatSize(5_378_934, prefix=bpColloquial, decimalSep=',') == "5,13MB"
|
|
||||||
##
|
|
||||||
const iecPrefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"]
|
const iecPrefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"]
|
||||||
const collPrefixes = ["", "k", "M", "G", "T", "P", "E", "Z", "Y"]
|
const collPrefixes = ["", "k", "M", "G", "T", "P", "E", "Z", "Y"]
|
||||||
var
|
var
|
||||||
|
|
@ -2156,7 +2149,7 @@ proc formatEng*(f: BiggestFloat,
|
||||||
## formatEng(4100, unit="V") == "4.1e3 V"
|
## formatEng(4100, unit="V") == "4.1e3 V"
|
||||||
## formatEng(4100, unit="") == "4.1e3 " # Space with unit=""
|
## formatEng(4100, unit="") == "4.1e3 " # Space with unit=""
|
||||||
##
|
##
|
||||||
## `decimalSep` is used as the decimal separator
|
## `decimalSep` is used as the decimal separator.
|
||||||
var
|
var
|
||||||
absolute: BiggestFloat
|
absolute: BiggestFloat
|
||||||
significand: BiggestFloat
|
significand: BiggestFloat
|
||||||
|
|
@ -2369,17 +2362,16 @@ proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.
|
||||||
rtl, extern: "nsuRemoveSuffixCharSet".} =
|
rtl, extern: "nsuRemoveSuffixCharSet".} =
|
||||||
## Removes all characters from `chars` from the end of the string `s`
|
## Removes all characters from `chars` from the end of the string `s`
|
||||||
## (in-place).
|
## (in-place).
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: nim
|
var userInput = "Hello World!*~\r\n"
|
||||||
## var userInput = "Hello World!*~\r\n"
|
userInput.removeSuffix
|
||||||
## userInput.removeSuffix
|
doAssert userInput == "Hello World!*~"
|
||||||
## doAssert userInput == "Hello World!*~"
|
userInput.removeSuffix({'~', '*'})
|
||||||
## userInput.removeSuffix({'~', '*'})
|
doAssert userInput == "Hello World!"
|
||||||
## doAssert userInput == "Hello World!"
|
|
||||||
##
|
var otherInput = "Hello!?!"
|
||||||
## var otherInput = "Hello!?!"
|
otherInput.removeSuffix({'!', '?'})
|
||||||
## otherInput.removeSuffix({'!', '?'})
|
doAssert otherInput == "Hello"
|
||||||
## doAssert otherInput == "Hello"
|
|
||||||
if s.len == 0: return
|
if s.len == 0: return
|
||||||
var last = s.high
|
var last = s.high
|
||||||
while last > -1 and s[last] in chars: last -= 1
|
while last > -1 and s[last] in chars: last -= 1
|
||||||
|
|
@ -2390,24 +2382,23 @@ proc removeSuffix*(s: var string, c: char) {.
|
||||||
## Removes all occurrences of a single character (in-place) from the end
|
## Removes all occurrences of a single character (in-place) from the end
|
||||||
## of a string.
|
## of a string.
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
runnableExamples:
|
||||||
## var table = "users"
|
var table = "users"
|
||||||
## table.removeSuffix('s')
|
table.removeSuffix('s')
|
||||||
## doAssert table == "user"
|
doAssert table == "user"
|
||||||
##
|
|
||||||
## var dots = "Trailing dots......."
|
var dots = "Trailing dots......."
|
||||||
## dots.removeSuffix('.')
|
dots.removeSuffix('.')
|
||||||
## doAssert dots == "Trailing dots"
|
doAssert dots == "Trailing dots"
|
||||||
removeSuffix(s, chars = {c})
|
removeSuffix(s, chars = {c})
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, suffix: string) {.
|
proc removeSuffix*(s: var string, suffix: string) {.
|
||||||
rtl, extern: "nsuRemoveSuffixString".} =
|
rtl, extern: "nsuRemoveSuffixString".} =
|
||||||
## Remove the first matching suffix (in-place) from a string.
|
## Remove the first matching suffix (in-place) from a string.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: nim
|
var answers = "yeses"
|
||||||
## var answers = "yeses"
|
answers.removeSuffix("es")
|
||||||
## answers.removeSuffix("es")
|
doAssert answers == "yes"
|
||||||
## doAssert answers == "yes"
|
|
||||||
var newLen = s.len
|
var newLen = s.len
|
||||||
if s.endsWith(suffix):
|
if s.endsWith(suffix):
|
||||||
newLen -= len(suffix)
|
newLen -= len(suffix)
|
||||||
|
|
@ -2418,16 +2409,16 @@ proc removePrefix*(s: var string, chars: set[char] = Newlines) {.
|
||||||
## Removes all characters from `chars` from the start of the string `s`
|
## Removes all characters from `chars` from the start of the string `s`
|
||||||
## (in-place).
|
## (in-place).
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
runnableExamples:
|
||||||
## var userInput = "\r\n*~Hello World!"
|
var userInput = "\r\n*~Hello World!"
|
||||||
## userInput.removePrefix
|
userInput.removePrefix
|
||||||
## doAssert userInput == "*~Hello World!"
|
doAssert userInput == "*~Hello World!"
|
||||||
## userInput.removePrefix({'~', '*'})
|
userInput.removePrefix({'~', '*'})
|
||||||
## doAssert userInput == "Hello World!"
|
doAssert userInput == "Hello World!"
|
||||||
##
|
|
||||||
## var otherInput = "?!?Hello!?!"
|
var otherInput = "?!?Hello!?!"
|
||||||
## otherInput.removePrefix({'!', '?'})
|
otherInput.removePrefix({'!', '?'})
|
||||||
## doAssert otherInput == "Hello!?!"
|
doAssert otherInput == "Hello!?!"
|
||||||
var start = 0
|
var start = 0
|
||||||
while start < s.len and s[start] in chars: start += 1
|
while start < s.len and s[start] in chars: start += 1
|
||||||
if start > 0: s.delete(0, start - 1)
|
if start > 0: s.delete(0, start - 1)
|
||||||
|
|
@ -2437,20 +2428,20 @@ proc removePrefix*(s: var string, c: char) {.
|
||||||
## Removes all occurrences of a single character (in-place) from the start
|
## Removes all occurrences of a single character (in-place) from the start
|
||||||
## of a string.
|
## of a string.
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
runnableExamples:
|
||||||
## var ident = "pControl"
|
var ident = "pControl"
|
||||||
## ident.removePrefix('p')
|
ident.removePrefix('p')
|
||||||
## doAssert ident == "Control"
|
doAssert ident == "Control"
|
||||||
removePrefix(s, chars = {c})
|
removePrefix(s, chars = {c})
|
||||||
|
|
||||||
proc removePrefix*(s: var string, prefix: string) {.
|
proc removePrefix*(s: var string, prefix: string) {.
|
||||||
rtl, extern: "nsuRemovePrefixString".} =
|
rtl, extern: "nsuRemovePrefixString".} =
|
||||||
## Remove the first matching prefix (in-place) from a string.
|
## Remove the first matching prefix (in-place) from a string.
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
runnableExamples:
|
||||||
## var answers = "yesyes"
|
var answers = "yesyes"
|
||||||
## answers.removePrefix("yes")
|
answers.removePrefix("yes")
|
||||||
## doAssert answers == "yes"
|
doAssert answers == "yes"
|
||||||
if s.startsWith(prefix):
|
if s.startsWith(prefix):
|
||||||
s.delete(0, prefix.len - 1)
|
s.delete(0, prefix.len - 1)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue