Remove dependency on `libicu` (#213)

pull/217/head
Mickaël Menu 2 years ago committed by GitHub
parent 1167cb99ae
commit 3c634fb00a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,7 +2,12 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
<!--## Unreleased--> ## Unreleased
### Changed
* Removed the dependency on `libicu`.
## 0.10.0 ## 0.10.0

@ -47,11 +47,11 @@ BUILD := `git rev-parse --short HEAD`
ENV_PREFIX := CGO_ENABLED=1 ENV_PREFIX := CGO_ENABLED=1
# Add necessary env variables for Apple Silicon. # Add necessary env variables for Apple Silicon.
ifeq ($(shell uname -sm),Darwin arm64) 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 endif
# Wrapper around the go binary, to set all the default parameters. # Wrapper around the go binary, to set all the default parameters.
define go 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 endef

@ -79,20 +79,14 @@ $ cd zk
#### On macOS #### On macOS
`icu4c` is required to build `zk`, which you can install with [Homebrew](https://brew.sh/).
``` ```
$ brew install icu4c
$ make $ make
$ ./zk -h $ ./zk -h
``` ```
#### On Linux #### On Linux
`libicu-dev` is required to build `zk`, use your favorite package manager to install it.
``` ```
$ apt-install libicu-dev
$ make $ make
$ ./zk -h $ ./zk -h
``` ```

@ -3,6 +3,7 @@ package sqlite
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"regexp"
sqlite "github.com/mattn/go-sqlite3" sqlite "github.com/mattn/go-sqlite3"
"github.com/mickael-menu/zk/internal/core" "github.com/mickael-menu/zk/internal/core"
@ -16,6 +17,9 @@ func init() {
if err := conn.RegisterFunc("mention_query", buildMentionQuery, true); err != nil { if err := conn.RegisterFunc("mention_query", buildMentionQuery, true); err != nil {
return err return err
} }
if err := conn.RegisterFunc("regexp", regexp.MatchString, true); err != nil {
return err
}
return nil return nil
}, },
}) })

@ -12,7 +12,6 @@ import (
"github.com/mickael-menu/zk/internal/util" "github.com/mickael-menu/zk/internal/util"
"github.com/mickael-menu/zk/internal/util/errors" "github.com/mickael-menu/zk/internal/util/errors"
"github.com/mickael-menu/zk/internal/util/fts5" "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/opt"
"github.com/mickael-menu/zk/internal/util/paths" "github.com/mickael-menu/zk/internal/util/paths"
strutil "github.com/mickael-menu/zk/internal/util/strings" 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. // matching a sub-section in the note.
href = strings.SplitN(href, "#", 2)[0] href = strings.SplitN(href, "#", 2)[0]
href = icu.EscapePattern(href) href = regexp.QuoteMeta(href)
if allowPartialHref { if allowPartialHref {
ids, err := d.findIdsByPathRegex("^(.*/)?[^/]*" + href + "[^/]*$") 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 { if len(ids) > 0 || err != nil {
return ids, err return ids, err
} }

@ -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
}

@ -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)
}
}
Loading…
Cancel
Save