Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strutils new sets #18193

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@

- Added `dom.scrollIntoView` proc with options

- Added `ControlChars`, `GraphicChars`, `PrintableChars`, and `Punctuation` sets to `strutils`.

## Language changes

- `nimscript` now handles `except Exception as e`.
Expand Down
29 changes: 20 additions & 9 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ from std/private/strimpl import cmpIgnoreStyleImpl, cmpIgnoreCaseImpl, startsWit


const
Whitespace* = {' ', '\t', '\v', '\r', '\l', '\f'}
Whitespace* = {' ', '\t', '\v', '\r', '\n', '\f'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, a separate PR would be welcome to change all instances of \l to \n in nim repo (and it'd only do that plus maybe related changes)

## All the characters that count as whitespace (space, tab, vertical tab,
## carriage return, new line, form feed).

Expand All @@ -94,21 +94,33 @@ const
Digits* = {'0'..'9'}
## The set of digits.

HexDigits* = {'0'..'9', 'A'..'F', 'a'..'f'}
HexDigits* = Digits + {'A'..'F', 'a'..'f'}
## The set of hexadecimal digits.

IdentChars* = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
IdentChars* = Letters + Digits + {'_'}
## The set of characters an identifier can consist of.

IdentStartChars* = {'a'..'z', 'A'..'Z', '_'}
IdentStartChars* = Letters + {'_'}
## The set of characters an identifier can start with.

Newlines* = {'\13', '\10'}
## The set of characters a newline terminator can start with (carriage
## return, line feed).
Newlines* = {'\r', '\n'}
## The set of characters a newline terminator can consist of

ControlChars* = {'\0'..'\31', '\127'}
## The set of all ASCII control characters.
kintrix007 marked this conversation as resolved.
Show resolved Hide resolved

GraphicChars* = {'!'..'~'}
## The set of all ASCII graphic characters.

PrintableChars* = GraphicChars + {' '}
## The set of all ASCII printable characters.

Punctuation* = {'!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'}
## The set of all ASCII punctuation characters.
## Is the same as `PrintableChars - Letters - Digits`
kintrix007 marked this conversation as resolved.
Show resolved Hide resolved

AllChars* = {'\x00'..'\xFF'}
## A set with all the possible characters.
## The set of all characters.
##
## Not very useful by its own, you can use it to create *inverted* sets to
## make the `find func<#find,string,set[char],Natural,int>`_
Expand Down Expand Up @@ -185,7 +197,6 @@ func isUpperAscii*(c: char): bool {.rtl, extern: "nsuIsUpperAsciiChar".} =
doAssert isUpperAscii('7') == false
return c in {'A'..'Z'}


func toLowerAscii*(c: char): char {.rtl, extern: "nsuToLowerAsciiChar".} =
## Returns the lower case version of character `c`.
##
Expand Down
27 changes: 27 additions & 0 deletions tests/stdlib/tstrutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ template rejectParse(e) =
except ValueError: discard

template main() =
# Tests for the sets

block: # Whitespace
proc isspace(c: cint): cint {.importc, header: "<ctype.h>".}
for i in char.low .. char.high:
doAssert (isspace(i.cint) > 0) == (i in Whitespace)

block: # ControlChars
proc iscntrl(c: cint): cint {.importc, header: "<ctype.h>".}
for i in char.low .. char.high:
doAssert (iscntrl(i.cint) > 0) == (i in ControlChars)

block: # GraphicChars
kintrix007 marked this conversation as resolved.
Show resolved Hide resolved
proc isgraph(c: cint): cint {.importc, header: "<ctype.h>".}
for i in char.low .. char.high:
doAssert (isgraph(i.cint) > 0) == (i in GraphicChars)

block: # PrintableChars
proc isprint(c: cint): cint {.importc, header: "<ctype.h>".}
for i in char.low .. char.high:
doAssert (isprint(i.cint) > 0) == (i in PrintableChars)

block: # Punctuation
proc ispunct(c: cint): cint {.importc, header: "<ctype.h>".}
for i in char.low .. char.high:
doAssert (ispunct(i.cint) > 0) == (i in Punctuation)

block: # strip
doAssert strip(" ha ") == "ha"
doAssert strip(" foofoofoo ") == "foofoofoo"
Expand Down