Add Wider Ascii Chars sets and func for string formatting (#19994)
* Add more Ascii Chars sets - add UpperCaseLetters set - add LowerCaseLetters set - add Punctuations set - add PrintablesNoWhiteSpace set - add Printables set - add isPunctuationAscii func - add isPrintableAscii func * Omit isPunctuationAscii and isPrintableAscii procs * Apply suggestions for adding Wider Ascii Chars sets Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com> * Update strutils.nim Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
This commit is contained in:
parent
313ce91533
commit
3987a3bf97
1 changed files with 25 additions and 12 deletions
|
|
@ -80,7 +80,8 @@ export toLower, toUpper
|
||||||
|
|
||||||
include "system/inclrtl"
|
include "system/inclrtl"
|
||||||
import std/private/since
|
import std/private/since
|
||||||
from std/private/strimpl import cmpIgnoreStyleImpl, cmpIgnoreCaseImpl, startsWithImpl, endsWithImpl
|
from std/private/strimpl import cmpIgnoreStyleImpl, cmpIgnoreCaseImpl,
|
||||||
|
startsWithImpl, endsWithImpl
|
||||||
|
|
||||||
when defined(nimPreviewSlimSystem):
|
when defined(nimPreviewSlimSystem):
|
||||||
import std/assertions
|
import std/assertions
|
||||||
|
|
@ -94,6 +95,18 @@ const
|
||||||
Letters* = {'A'..'Z', 'a'..'z'}
|
Letters* = {'A'..'Z', 'a'..'z'}
|
||||||
## The set of letters.
|
## The set of letters.
|
||||||
|
|
||||||
|
UppercaseLetters* = {'A'..'Z'}
|
||||||
|
## The set of uppercase ASCII letters.
|
||||||
|
|
||||||
|
LowercaseLetters* = {'a'..'z'}
|
||||||
|
## The set of lowercase ASCII letters.
|
||||||
|
|
||||||
|
PunctuationChars* = {'!'..'/', ':'..'@', '['..'`', '{'..'~'}
|
||||||
|
## The set of all ASCII punctuation characters.
|
||||||
|
|
||||||
|
PrintableChars* = Letters + Digits + PunctuationChars + Whitespace
|
||||||
|
## The set of all printable ASCII characters (letters, digits, whitespace, and punctuation characters).
|
||||||
|
|
||||||
Digits* = {'0'..'9'}
|
Digits* = {'0'..'9'}
|
||||||
## The set of digits.
|
## The set of digits.
|
||||||
|
|
||||||
|
|
@ -172,7 +185,7 @@ func isLowerAscii*(c: char): bool {.rtl, extern: "nsuIsLowerAsciiChar".} =
|
||||||
doAssert isLowerAscii('e') == true
|
doAssert isLowerAscii('e') == true
|
||||||
doAssert isLowerAscii('E') == false
|
doAssert isLowerAscii('E') == false
|
||||||
doAssert isLowerAscii('7') == false
|
doAssert isLowerAscii('7') == false
|
||||||
return c in {'a'..'z'}
|
return c in LowercaseLetters
|
||||||
|
|
||||||
func isUpperAscii*(c: char): bool {.rtl, extern: "nsuIsUpperAsciiChar".} =
|
func isUpperAscii*(c: char): bool {.rtl, extern: "nsuIsUpperAsciiChar".} =
|
||||||
## Checks whether or not `c` is an upper case character.
|
## Checks whether or not `c` is an upper case character.
|
||||||
|
|
@ -186,8 +199,7 @@ func isUpperAscii*(c: char): bool {.rtl, extern: "nsuIsUpperAsciiChar".} =
|
||||||
doAssert isUpperAscii('e') == false
|
doAssert isUpperAscii('e') == false
|
||||||
doAssert isUpperAscii('E') == true
|
doAssert isUpperAscii('E') == true
|
||||||
doAssert isUpperAscii('7') == false
|
doAssert isUpperAscii('7') == false
|
||||||
return c in {'A'..'Z'}
|
return c in UppercaseLetters
|
||||||
|
|
||||||
|
|
||||||
func toLowerAscii*(c: char): char {.rtl, extern: "nsuToLowerAsciiChar".} =
|
func toLowerAscii*(c: char): char {.rtl, extern: "nsuToLowerAsciiChar".} =
|
||||||
## Returns the lower case version of character `c`.
|
## Returns the lower case version of character `c`.
|
||||||
|
|
@ -202,7 +214,7 @@ func toLowerAscii*(c: char): char {.rtl, extern: "nsuToLowerAsciiChar".} =
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
doAssert toLowerAscii('A') == 'a'
|
doAssert toLowerAscii('A') == 'a'
|
||||||
doAssert toLowerAscii('e') == 'e'
|
doAssert toLowerAscii('e') == 'e'
|
||||||
if c in {'A'..'Z'}:
|
if c in UppercaseLetters:
|
||||||
result = char(uint8(c) xor 0b0010_0000'u8)
|
result = char(uint8(c) xor 0b0010_0000'u8)
|
||||||
else:
|
else:
|
||||||
result = c
|
result = c
|
||||||
|
|
@ -239,7 +251,7 @@ func toUpperAscii*(c: char): char {.rtl, extern: "nsuToUpperAsciiChar".} =
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
doAssert toUpperAscii('a') == 'A'
|
doAssert toUpperAscii('a') == 'A'
|
||||||
doAssert toUpperAscii('E') == 'E'
|
doAssert toUpperAscii('E') == 'E'
|
||||||
if c in {'a'..'z'}:
|
if c in LowercaseLetters:
|
||||||
result = char(uint8(c) xor 0b0010_0000'u8)
|
result = char(uint8(c) xor 0b0010_0000'u8)
|
||||||
else:
|
else:
|
||||||
result = c
|
result = c
|
||||||
|
|
@ -289,7 +301,7 @@ func nimIdentNormalize*(s: string): string =
|
||||||
result[0] = s[0]
|
result[0] = s[0]
|
||||||
var j = 1
|
var j = 1
|
||||||
for i in 1..len(s) - 1:
|
for i in 1..len(s) - 1:
|
||||||
if s[i] in {'A'..'Z'}:
|
if s[i] in UppercaseLetters:
|
||||||
result[j] = chr(ord(s[i]) + (ord('a') - ord('A')))
|
result[j] = chr(ord(s[i]) + (ord('a') - ord('A')))
|
||||||
inc j
|
inc j
|
||||||
elif s[i] != '_':
|
elif s[i] != '_':
|
||||||
|
|
@ -311,7 +323,7 @@ func normalize*(s: string): string {.rtl, extern: "nsuNormalize".} =
|
||||||
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:
|
||||||
if s[i] in {'A'..'Z'}:
|
if s[i] in UppercaseLetters:
|
||||||
result[j] = chr(ord(s[i]) + (ord('a') - ord('A')))
|
result[j] = chr(ord(s[i]) + (ord('a') - ord('A')))
|
||||||
inc j
|
inc j
|
||||||
elif s[i] != '_':
|
elif s[i] != '_':
|
||||||
|
|
@ -1515,7 +1527,8 @@ func delete*(s: var string, slice: Slice[int]) =
|
||||||
inc(j)
|
inc(j)
|
||||||
setLen(s, newLen)
|
setLen(s, newLen)
|
||||||
|
|
||||||
func delete*(s: var string, first, last: int) {.rtl, extern: "nsuDelete", deprecated: "use `delete(s, first..last)`".} =
|
func delete*(s: var string, first, last: int) {.rtl, extern: "nsuDelete",
|
||||||
|
deprecated: "use `delete(s, first..last)`".} =
|
||||||
## Deletes in `s` the characters at positions `first .. last` (both ends included).
|
## Deletes in `s` the characters at positions `first .. last` (both ends included).
|
||||||
runnableExamples("--warning:deprecated:off"):
|
runnableExamples("--warning:deprecated:off"):
|
||||||
var a = "abracadabra"
|
var a = "abracadabra"
|
||||||
|
|
@ -2354,7 +2367,7 @@ func validIdentifier*(s: string): bool {.rtl, extern: "nsuValidIdentifier".} =
|
||||||
# floating point formatting:
|
# floating point formatting:
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
func c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
|
func c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
|
||||||
importc: "sprintf", varargs}
|
importc: "sprintf", varargs.}
|
||||||
|
|
||||||
type
|
type
|
||||||
FloatFormatMode* = enum
|
FloatFormatMode* = enum
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue