diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/text.lux | 27 | ||||
-rw-r--r-- | stdlib/source/lux/language/compiler/extension/analysis/common.lux | 1 |
2 files changed, 24 insertions, 4 deletions
diff --git a/stdlib/source/lux/data/text.lux b/stdlib/source/lux/data/text.lux index 6fc05aa9c..6106e434e 100644 --- a/stdlib/source/lux/data/text.lux +++ b/stdlib/source/lux/data/text.lux @@ -8,8 +8,12 @@ [codec (#+ Codec)] hash] [data - [collection [list ("list/" Fold<List>)]] - [maybe]]]) + [bit] + [maybe] + [collection + [list ("list/" Fold<List>)]]] + [language + [host]]]) (def: #export (size x) (-> Text Nat) @@ -166,7 +170,24 @@ (def: eq Equivalence<Text>) (def: (hash input) - ("lux text hash" input))) + (`` (for {(~~ (static host.jvm)) + (|> input + (: (primitive "java.lang.String" [])) + "jvm invokevirtual:java.lang.String:hashCode:" + "jvm convert int-to-long" + (:coerce Nat))} + ## Platform-independent default. + (let [length ("lux text size" input)] + (loop [idx +0 + hash +0] + (if (n/< length idx) + (let [char (|> idx ("lux text char" input) (maybe.default +0))] + (recur (inc idx) + (|> hash + (bit.left-shift +5) + (n/- hash) + (n/+ char)))) + hash))))))) (def: #export concat (-> (List Text) Text) diff --git a/stdlib/source/lux/language/compiler/extension/analysis/common.lux b/stdlib/source/lux/language/compiler/extension/analysis/common.lux index 1a377ec14..0dac69ced 100644 --- a/stdlib/source/lux/language/compiler/extension/analysis/common.lux +++ b/stdlib/source/lux/language/compiler/extension/analysis/common.lux @@ -204,7 +204,6 @@ (///bundle.install "concat" (binary Text Text Text)) (///bundle.install "index" (trinary Text Text Nat (type (Maybe Nat)))) (///bundle.install "size" (unary Text Nat)) - (///bundle.install "hash" (unary Text Nat)) (///bundle.install "char" (binary Text Nat (type (Maybe Nat)))) (///bundle.install "clip" (trinary Text Nat Nat (type (Maybe Text)))) ))) |