-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add strutils in-place #13610
Add strutils in-place #13610
Changes from 5 commits
f23fdc2
881a24e
41a1579
c9c0c6b
e25dad3
b59af3f
461014c
6e51e2e
45c99ad
5b97248
546f9d0
94d9d34
5497b4c
82cf6a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2860,6 +2860,79 @@ proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, | |||||||||
## Alias for isEmptyOrWhitespace | ||||||||||
result = isEmptyOrWhitespace(s) | ||||||||||
|
||||||||||
|
||||||||||
since (1, 1): | ||||||||||
template toLowerAsciiInPlace(c: var char) = | ||||||||||
if c in {'A'..'Z'}: c = chr(ord(c) + (ord('a') - ord('A'))) | ||||||||||
|
||||||||||
template toUpperAsciiInPlace(c: var char) = | ||||||||||
if c in {'a'..'z'}: c = chr(ord(c) - (ord('a') - ord('A'))) | ||||||||||
|
||||||||||
|
||||||||||
func toLowerAsciiInPlace*(s: var string) {.inline.} = | ||||||||||
## Converts string `s` into lower case. | ||||||||||
## | ||||||||||
## This works only for the letters ``A-Z``. See `unicode.toLower | ||||||||||
## <unicode.html#toLower,string>`_ for a version that works for any Unicode | ||||||||||
## character. | ||||||||||
## | ||||||||||
## See also: | ||||||||||
## * `normalize proc<#normalize,string>`_ | ||||||||||
runnableExamples: | ||||||||||
var stringy = "ABCDEF" | ||||||||||
toLowerAsciiInPlace(stringy) | ||||||||||
doAssert stringy == "abcdef" | ||||||||||
var strng = "NIM" | ||||||||||
toLowerAsciiInPlace(strng) | ||||||||||
doAssert strng == "nim" | ||||||||||
var i = 0 | ||||||||||
for c in mitems(s): | ||||||||||
toLowerAsciiInPlace(c) | ||||||||||
s[i] = c | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's in-place already, so why assigning it again? |
||||||||||
inc i | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You're using |
||||||||||
|
||||||||||
|
||||||||||
func toUpperAsciiInPlace*(s: var string) {.inline.} = | ||||||||||
## Converts string `s` into upper case. | ||||||||||
## | ||||||||||
## This works only for the letters ``A-Z``. See `unicode.toUpper | ||||||||||
## <unicode.html#toUpper,string>`_ for a version that works for any Unicode | ||||||||||
## character. | ||||||||||
## | ||||||||||
## See also: | ||||||||||
## * `capitalizeAscii proc<#capitalizeAscii,string>`_ | ||||||||||
runnableExamples: | ||||||||||
var stringo = "abcdef" | ||||||||||
toUpperAsciiInPlace(stringo) | ||||||||||
doAssert stringo == "ABCDEF" | ||||||||||
var strng = "nim" | ||||||||||
toUpperAsciiInPlace(strng) | ||||||||||
doAssert strng == "NIM" | ||||||||||
var i = 0 | ||||||||||
for c in mitems(s): | ||||||||||
toUpperAsciiInPlace(c) | ||||||||||
s[i] = c | ||||||||||
inc i | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
|
||||||||||
func capitalizeAsciiInPlace*(s: var string) {.inline.} = | ||||||||||
## Converts the first character of string `s` into upper case. | ||||||||||
## | ||||||||||
## This works only for the letters ``A-Z``. | ||||||||||
## Use `Unicode module<unicode.html>`_ for UTF-8 support. | ||||||||||
## | ||||||||||
## See also: | ||||||||||
## * `toUpperAscii proc<#toUpperAscii,char>`_ | ||||||||||
runnableExamples: | ||||||||||
var stringu = "foo" | ||||||||||
capitalizeAsciiInPlace(stringu) | ||||||||||
doAssert stringu == "Foo" | ||||||||||
var stringx = "-bar" | ||||||||||
capitalizeAsciiInPlace(stringx) | ||||||||||
doAssert stringx == "-bar" | ||||||||||
toUpperAsciiInPlace(s[0]) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
|
||||||||||
when isMainModule: | ||||||||||
proc nonStaticTests = | ||||||||||
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000" | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add
{.inline.}
to the currenttoLowerAscii
andtoUpperAscii
instead of duplicating the code here. It's demonstrated that the performance gains of this duplication is negligible compared to when you add{.inline.}
to the current versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.