https://github.com/pragmagic/names
String interning library for Nim
https://github.com/pragmagic/names
nim strings
Last synced: 2 months ago
JSON representation
String interning library for Nim
- Host: GitHub
- URL: https://github.com/pragmagic/names
- Owner: pragmagic
- License: other
- Created: 2018-02-18T18:10:28.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-19T18:06:42.000Z (over 8 years ago)
- Last Synced: 2025-04-09T16:17:52.246Z (about 1 year ago)
- Topics: nim, strings
- Language: Nim
- Size: 3.91 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# names
This module implements `Name` type, which is an interned string, allowing
fast comparison and hash retrieval.
Consider using this type when:
1) You would like to use `string` as a key of a hash table.
2) Many equal strings can be created in different parts of your application.
API:
```nim
type Name* = ref object
## Represents an interned string allowing fast equality and hash operations.
proc toName*(s: string): Name
## Converts the ``string`` to ``Name``.
## Returns ``nil`` if ``s`` is ``nil``.
proc toName*(s: cstring): Name
## Converts the null-terminated ``cstring`` to ``Name``.
## Returns ``nil`` if ``s`` is ``nil``.
proc toExistingName*(s: string): Name
## Returns ``Name`` corresponding to the string, but only if the name
## is already registered. Otherwise returns ``nil``.
## This is useful when you don't want to pollute the name table with
## unverified user input.
proc toExistingName*(s: cstring): Name
## Returns ``Name`` corresponding to the string, but only if the name
## is already registered. Otherwise returns ``nil``.
## This is useful when you don't want to pollute the name table with
## unverified user input.
proc isValid*(name: Name): bool
## Returns ``true`` if the ``name`` is initialized, ``false`` otherwise.
## A name returned from ``toName`` call with a non-nil argument is always
## valid.
proc `==`*(n1, n2: Name): bool
## Returns ``true`` if the names are equal, ``false`` otherwise.
proc hash*(name: Name): Hash
## Returns hash of the ``name``. The hash may not be equal to the hash
## of the string the name represents.
proc cmp*(n1, n2: Name): int
## Returns 0 if the names are equal, a value < 0 if n1 < n2, and
## a value > 0 otherwise.
## The comparison is lexicographic.
proc `<`*(n1, n2: Name): bool
## Returns ``true`` if ``n1`` < ``n2``, ``false`` otherwise.
## The comparison is lexicographic.
template `$`*(name: Name): string
## Returns string representation of the ``name`` or ``nil``, if
## the ``name`` is not valid.
## Implemented as a template to avoid redundant string copies - the
## underlying string is always immutable, so it gets copied only if you
## assign the result to a mutable (``var``) variable.
```
## License
This library is licensed under the MIT license.
Read [LICENSE](LICENSE) file for details.
Copyright (c) 2018 Xored Software, Inc.