-
-
Notifications
You must be signed in to change notification settings - Fork 174
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
Dehyphen improvements #387
Comments
I looked at that doc page and the syntax looks the same as for the JVM regex -- what am I missing? |
Heh, somewhat to my surprised, I changed So here's (defn- dehyphen
"Replace _embedded_ hyphens with spaces.
Hyphens at the start or end of a string should not be touched."
[s]
(str/replace s #"(\w)-(\w)" "$1 $2")) and here are the tests: (deftest sql-kw-test
(is (= "FETCH NEXT" (sut/sql-kw :fetch-next)))
(is (= "WHAT IS THIS" (sut/sql-kw :what-is-this)))
(is (= "FEE FIE FOE FUM" (sut/sql-kw :fee-fie-foe-fum)))
(is (= "-WHAT THE-" (sut/sql-kw :-what-the-)))
(is (= "fetch_next" (sut/sql-kw :'fetch-next)))
(is (= "what_is_this" (sut/sql-kw :'what-is-this)))
(is (= "fee_fie_foe_fum" (sut/sql-kw :'fee-fie-foe-fum)))
(is (= "_what_the_" (sut/sql-kw :'-what-the-)))) and... they all pass straight away... so the loop was never required apparently. |
This is weird, why isn't it working as expected? (clojure.string/replace "a-b-c-d" #"(\w)-(\w)" "$1 $2")
"a b-c d" EDIT: I get it, it's an edge case where the word is one character long |
Yeah, took me a while to realize that's what the issue was: dev=> (#'sql/dehyphen :a-b-c)
":a b-c"
dev=> (#'sql/dehyphen :a-bx-c)
":a bx c" |
FWIW, the regex you provided causes these failures (which the previous loop/regex did not): FAIL in (sql-kw-test) (sql_test.cljc:872)
expected: (= "-X" (sut/sql-kw :-x))
actual: (not (= "-X" " X"))
FAIL in (sql-kw-test) (sql_test.cljc:874)
expected: (= "-X-" (sut/sql-kw :-x-))
actual: (not (= "-X-" " X-"))
FAIL in (sql-kw-test) (sql_test.cljc:881)
expected: (= "-WHAT THE-" (sut/sql-kw :-what-the-))
actual: (not (= "-WHAT THE-" " WHAT THE-")) |
This variant seems to work: |
I'm not going to cut a new release just for this fix unless someone trips over the single character edge case. |
the dehyphen docstring mentions there's probably a faster solution.
The equivalent solution in JVM regex would be:
(str/replace s #"(?!\w)-(?=\w)" " ")
Javascript also support lookahead/back, but with different syntax https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions
Rough benchmarks show this regex is 2x faster on the JVM
The text was updated successfully, but these errors were encountered: