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 5 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`, `strutils.capitalizeAsciiInPlace`,
faster than the copy versions, at least from ~1.5x to ~5x performance approx.


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

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 current toLowerAscii and toUpperAscii 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.



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

Choose a reason for hiding this comment

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

It's in-place already, so why assigning it again?

inc i
Copy link
Collaborator

@alaviss alaviss Mar 10, 2020

Choose a reason for hiding this comment

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

Suggested change
toLowerAsciiInPlace(c)
s[i] = c
inc i
c = toLowerAscii(c)

You're using mitems, which allows you to directly modify the elements of the passed string.



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

Choose a reason for hiding this comment

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

Suggested change
toUpperAsciiInPlace(c)
s[i] = c
inc i
c = toUpperAscii(c)



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])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
toUpperAsciiInPlace(s[0])
s[0] = toUpperAscii(s[0])



when isMainModule:
proc nonStaticTests =
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
Expand Down