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

Add strutils in-place #13610

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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 @@ -94,6 +94,8 @@ echo f
- Added `times.isLeapDay`
- Added a new module, `std / compilesettings` for querying the compiler about
diverse configuration settings.
- Added in-place `strutils.toLowerAsciiInPlace`, `strutils.toUpperAsciiInPlace`,
faster than the copy versions, at least from ~1.5x to ~5x performance approx.


## Library changes
Expand Down
38 changes: 38 additions & 0 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

Copy link
Collaborator Author

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 🤷‍♂️

Copy link
Member

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.

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
Copy link
Member

Choose a reason for hiding this comment

The 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"
Expand Down