From 3c634fb00a269411c7ffbe1c4949196aa1405d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Sat, 14 May 2022 10:44:39 +0200 Subject: [PATCH] Remove dependency on `libicu` (#213) --- CHANGELOG.md | 7 ++++++- Makefile | 4 ++-- README.md | 6 ------ internal/adapter/sqlite/db.go | 4 ++++ internal/adapter/sqlite/note_dao.go | 5 ++--- internal/util/icu/icu.go | 19 ----------------- internal/util/icu/icu_test.go | 32 ----------------------------- 7 files changed, 14 insertions(+), 63 deletions(-) delete mode 100644 internal/util/icu/icu.go delete mode 100644 internal/util/icu/icu_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ed522f..2763a09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ All notable changes to this project will be documented in this file. - +## Unreleased + +### Changed + +* Removed the dependency on `libicu`. + ## 0.10.0 diff --git a/Makefile b/Makefile index ac6627a..895b399 100644 --- a/Makefile +++ b/Makefile @@ -47,11 +47,11 @@ BUILD := `git rev-parse --short HEAD` ENV_PREFIX := CGO_ENABLED=1 # Add necessary env variables for Apple Silicon. ifeq ($(shell uname -sm),Darwin arm64) - ENV_PREFIX := $(ENV) GOARCH=arm64 CGO_CFLAGS="-I/opt/homebrew/opt/icu4c/include" CGO_LDFLAGS="-L/opt/homebrew/opt/icu4c/lib" + ENV_PREFIX := $(ENV) GOARCH=arm64 endif # Wrapper around the go binary, to set all the default parameters. define go - $(ENV_PREFIX) go $(1) -tags "fts5 icu" -ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)" $(2) + $(ENV_PREFIX) go $(1) -tags "fts5" -ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)" $(2) endef diff --git a/README.md b/README.md index 54ec71d..6d3147e 100644 --- a/README.md +++ b/README.md @@ -79,20 +79,14 @@ $ cd zk #### On macOS -`icu4c` is required to build `zk`, which you can install with [Homebrew](https://brew.sh/). - ``` -$ brew install icu4c $ make $ ./zk -h ``` #### On Linux -`libicu-dev` is required to build `zk`, use your favorite package manager to install it. - ``` -$ apt-install libicu-dev $ make $ ./zk -h ``` diff --git a/internal/adapter/sqlite/db.go b/internal/adapter/sqlite/db.go index 276c596..bc24b0d 100644 --- a/internal/adapter/sqlite/db.go +++ b/internal/adapter/sqlite/db.go @@ -3,6 +3,7 @@ package sqlite import ( "database/sql" "fmt" + "regexp" sqlite "github.com/mattn/go-sqlite3" "github.com/mickael-menu/zk/internal/core" @@ -16,6 +17,9 @@ func init() { if err := conn.RegisterFunc("mention_query", buildMentionQuery, true); err != nil { return err } + if err := conn.RegisterFunc("regexp", regexp.MatchString, true); err != nil { + return err + } return nil }, }) diff --git a/internal/adapter/sqlite/note_dao.go b/internal/adapter/sqlite/note_dao.go index 574b324..0ce3c8b 100644 --- a/internal/adapter/sqlite/note_dao.go +++ b/internal/adapter/sqlite/note_dao.go @@ -12,7 +12,6 @@ import ( "github.com/mickael-menu/zk/internal/util" "github.com/mickael-menu/zk/internal/util/errors" "github.com/mickael-menu/zk/internal/util/fts5" - "github.com/mickael-menu/zk/internal/util/icu" "github.com/mickael-menu/zk/internal/util/opt" "github.com/mickael-menu/zk/internal/util/paths" strutil "github.com/mickael-menu/zk/internal/util/strings" @@ -284,7 +283,7 @@ func (d *NoteDAO) FindIdsByHref(href string, allowPartialHref bool) ([]core.Note // matching a sub-section in the note. href = strings.SplitN(href, "#", 2)[0] - href = icu.EscapePattern(href) + href = regexp.QuoteMeta(href) if allowPartialHref { ids, err := d.findIdsByPathRegex("^(.*/)?[^/]*" + href + "[^/]*$") @@ -298,7 +297,7 @@ func (d *NoteDAO) FindIdsByHref(href string, allowPartialHref bool) ([]core.Note } } - ids, err := d.findIdsByPathRegex(href + "[^/]*|" + href + "/.+") + ids, err := d.findIdsByPathRegex("^(?:" + href + "[^/]*|" + href + "/.+)$") if len(ids) > 0 || err != nil { return ids, err } diff --git a/internal/util/icu/icu.go b/internal/util/icu/icu.go deleted file mode 100644 index 6938029..0000000 --- a/internal/util/icu/icu.go +++ /dev/null @@ -1,19 +0,0 @@ -package icu - -// EscapePattern adds backslash escapes to protect any characters that would -// match as ICU pattern metacharacters. -// -// http://userguide.icu-project.org/strings/regexp -func EscapePattern(s string) string { - out := "" - - for _, c := range s { - switch c { - case '\\', '.', '^', '$', '(', ')', '[', ']', '{', '}', '|', '*', '+', '?': - out += `\` - } - out += string(c) - } - - return out -} diff --git a/internal/util/icu/icu_test.go b/internal/util/icu/icu_test.go deleted file mode 100644 index 685dcab..0000000 --- a/internal/util/icu/icu_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package icu - -import ( - "testing" - - "github.com/mickael-menu/zk/internal/util/test/assert" -) - -func TestEscapePAttern(t *testing.T) { - tests := map[string]string{ - `foo bar`: `foo bar`, - `\a`: `\\a`, - `.`: `\.`, - `^`: `\^`, - `$`: `\$`, - `(`: `\(`, - `)`: `\)`, - `[`: `\[`, - `]`: `\]`, - `{`: `\{`, - `}`: `\}`, - `|`: `\|`, - `*`: `\*`, - `+`: `\+`, - `?`: `\?`, - `(?:[A-Za-z0-9]+[._]?){1,}[A-Za-z0-9]+\@(?:(?:[A-Za-z0-9]+[-]?){1,}[A-Za-z0-9]+\.){1,}`: `\(\?:\[A-Za-z0-9\]\+\[\._\]\?\)\{1,\}\[A-Za-z0-9\]\+\\@\(\?:\(\?:\[A-Za-z0-9\]\+\[-\]\?\)\{1,\}\[A-Za-z0-9\]\+\\\.\)\{1,\}`, - } - - for input, expected := range tests { - assert.Equal(t, EscapePattern(input), expected) - } -}