-
-
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
Closed
Closed
Add strutils in-place #13610
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f23fdc2
Add strutils in-place with Docs and Examples
juancarlospaco 881a24e
Fix wrong overload picking up
juancarlospaco 41a1579
Remove linearScanEnd from in-place strutils
juancarlospaco c9c0c6b
Change algo as suggested
juancarlospaco e25dad3
Make char version of toLowerAscii/toUpperAscii private
juancarlospaco b59af3f
Make char version of toLowerAscii/toUpperAscii private
juancarlospaco 461014c
Add inline to toLowerAscii/toUpperAscii
juancarlospaco 6e51e2e
Add inline to toLowerAscii/toUpperAscii
juancarlospaco 45c99ad
Add inline to toLowerAscii/toUpperAscii
juancarlospaco 5b97248
Add inline to toLowerAscii/toUpperAscii
juancarlospaco 546f9d0
Improve algo, improve doc test
juancarlospaco 94d9d34
Improve algo, improve doc test
juancarlospaco 5497b4c
Use ord and sums
juancarlospaco 82cf6a5
Use ord and sums
juancarlospaco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2860,6 +2860,44 @@ proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, | |
## Alias for isEmptyOrWhitespace | ||
result = isEmptyOrWhitespace(s) | ||
|
||
|
||
since (1, 1): | ||
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 x = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ*+,-./:;<=>?@[]_{|}~" | ||
toLowerAsciiInPlace(x) | ||
doAssert x == "abcdefghijklmnopqrstuvwxyz01234567890abcdefghijklmnopqrstuvwxyz*+,-./:;<=>?@[]_{|}~" | ||
const t = "abcdefghijklmnopqrstuvwxyz".indent(65).cstring | ||
for c in mitems(s): | ||
if c in {'A'..'Z'}: c = t[c.ord] | ||
|
||
|
||
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 z = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ*+,-./:;<=>?@[]_{|}~" | ||
toUpperAsciiInPlace(z) | ||
doAssert z == "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ*+,-./:;<=>?@[]_{|}~" | ||
const t = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indent(97).cstring | ||
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. Same. |
||
for c in mitems(s): | ||
if c in {'a'..'z'}: c = t[c.ord] | ||
|
||
|
||
when isMainModule: | ||
proc nonStaticTests = | ||
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Silly, pointless lookup table. Instead use
c = chr(ord(c) - ord('A') + ord('a'))
. Basic math, it's a thing.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.
is slower on my bench, but if you say so 🤷♂️
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.
Microbenchmarks are misleading, in reality you can also have I-cache problems and then you appreciate not everything was "optimized" by 8 times loop unrolling with SSE instructions and 1KB lookup tables.