Skip to content

Commit bca08b0

Browse files
add info about equality, hashes, and help text (#69)
* add info about equality, hashes, and help text * Update README.md Co-authored-by: Nick Robinson <[email protected]> * Remove extra line --------- Co-authored-by: Nick Robinson <[email protected]>
1 parent dafec6f commit bca08b0

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,77 @@ and length. See the docstring for any individual InlineString type for more deta
5151
To generically turn any string into the smallest possible InlineString type,
5252
call `InlineString(x)`. To convert any iterator/array of strings to a single,
5353
promoted `InlineString` type, the `inlinestrings(A)` utility function is exported.
54+
55+
#### Equality
56+
57+
An inline string is value equal (`==`), but not identical equal (`===`) to the corresponding `String`:
58+
59+
```julia-repl
60+
61+
julia> i = String15("abc")
62+
"abc"
63+
64+
julia> s = "abc"
65+
"abc"
66+
67+
julia> i == s
68+
true
69+
70+
julia> i === s
71+
false
72+
```
73+
74+
#### Dictionaries & Hashes
75+
76+
The hash value of an inline string is the same as it's `String` counterpart. Continuing the above example:
77+
78+
```julia-repl
79+
julia> Dict(s => 1, i => 2)
80+
Dict{AbstractString, Int64} with 1 entry:
81+
String15("abc") => 2
82+
83+
julia> y[i], y[s]
84+
(2, 2)
85+
86+
julia> Dict(i => 1, i => 2)
87+
Dict{String15, Int64} with 1 entry:
88+
"abc" => 2
89+
90+
```
91+
92+
Note how the `Dict` is passed two key-value pairs but the result contains only one.
93+
This is because we've only given the dictionary a single key (`Dict`s use the object's hash as it's underlying key), since `i` and `s` implement the same hash:
94+
95+
```julia-repl
96+
julia> hash(i)
97+
0x7690a66f302b3f70
98+
99+
julia> hash(s)
100+
0x7690a66f302b3f70
101+
```
102+
103+
#### Additional details
104+
105+
Use the REPL's help mode to see more details, such as those desribed here for `String15`:
106+
107+
```julia-repl
108+
help?> String15
109+
110+
String15(str::AbstractString)
111+
String15(bytes::AbstractVector{UInt8}, pos, len)
112+
String15(ptr::Ptr{UInt8}, [len])
113+
114+
115+
Custom fixed-size string with a fixed size of 16 bytes. 1 byte is used to store the length of the string. If an inline
116+
string is shorter than 15 bytes, the entire string still occupies the full 16 bytes since they are, by definition,
117+
fixed size. Otherwise, they can be treated just like normal String values. Note that sizeof(x) will return the # of
118+
codeunits in an String15 like String, not the total fixed size. For the fixed size, call sizeof(String15). String15 can
119+
be constructed from an existing String (String15(x::AbstractString)), from a byte buffer with position and length
120+
(String15(buf, pos, len)), from a pointer with optional length (String15(ptr, len)) or built iteratively by starting
121+
with x = String15() and calling x, overflowed = InlineStrings.addcodeunit(x, b::UInt8) which returns a new String15
122+
with the new codeunit b appended and an overflowed Bool value indicating whether too many codeunits have been appended
123+
for the fixed size. When constructed from a pointer, note that the ptr must point to valid memory or program data may
124+
become corrupt. If the len argument is specified with the pointer, it must fit within the fixed size of String15; if no
125+
length is provided, the C-string is assumed to be NUL-terminated. If the NUL-terminated string ends up longer than can
126+
fit in String15, an ArgumentError will be thrown.
127+
```

0 commit comments

Comments
 (0)