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

misc cmpIgnoreStyle + friends #525

Open
timotheecour opened this issue Jan 13, 2021 · 9 comments
Open

misc cmpIgnoreStyle + friends #525

timotheecour opened this issue Jan 13, 2021 · 9 comments

Comments

@timotheecour timotheecour mentioned this issue Jan 13, 2021
1 task
@timotheecour
Copy link
Owner Author

  • lots of procs in std/private/strimpl should use a performance shortcut for the not-uncommon case where a == b (for the cstring case)

@timotheecour
Copy link
Owner Author

  • cmpIgnoreStyleImpl is an exported template that uses return, IMO it's bad style and should be changed to:
template cmpIgnoreStyleImpl*[T: string | cstring](a, b: T, firstCharCaseSensitive: static bool = false): int =
  var result = 0
  ...
  result

otherwise its use is error prone and violates user expectations for control flow.

(if it were a non-exported template, it would be a bit less problematic since the code is self contained in same module)

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 13, 2021

  • all procs taking cstring must handle nil properly, eg this fails:
import std/cstrutils
echo cmpIgnoreStyle("", nil) # SIGSEGV

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 13, 2021

  • low priority: check whether some procs can use nimCmpMem + strchr for performance

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 13, 2021

  • cstrutils.cmpIgnoreStyle doesn't ignore the case of 1st char, maybe we should deprecated it
    (ditto with friends)

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 13, 2021

  • cmpIgnoreStyleImpl: change default firstCharCaseSensitive=false to firstCharCaseSensitive=true (less error prone, more consistent with nim's notion of style insensitivity) and adjust client code accordingly

@timotheecour
Copy link
Owner Author

  • we should have tests for procs in lib/std/private/strimpl.nim, even if it's a private module

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 13, 2021

  • idents.cmpIgnoreStyle should also be refactored; but its implementation seems suspicious eg:
proc cmpIgnoreStyle*(a, b: cstring, blen: int): int =
  if a[0] != b[0]: return 1
  ...

=> why 1?

@timotheecour
Copy link
Owner Author

  • potential optimization for both cmpIgnoreStyle and cmpIgnoreCaseImpl (same/similar to one as done in getIdent): 1st check if nimCmpMem(a,b) == 0, in which case return 0 immediately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant