Skip to content
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

build: remove awk #36

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
*.txt

vendor/
/removecomments
/removecomments
/snake2camel
18 changes: 8 additions & 10 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ clean:
.PHONY: images
images: docs/urn.png

.PHONY: snake2camel
snake2camel:
@cd ./tools/snake2camel; go build -o ../../snake2camel .

.PHONY: removecomments
removecomments:
@cd ./tools/removecomments; go build -o ../../removecomments .

machine.go: machine.go.rl

machine.go: snake2camel

machine.go: removecomments

machine.go:
$(RAGEL) -Z -G2 -e -o $@ $<
@./removecomments $@
$(MAKE) -s file=$@ snake2camel
@./snake2camel $@
$(GOFMT) $@

docs/urn.dot: machine.go.rl
Expand All @@ -41,13 +47,5 @@ bench: *_test.go machine.go
go test -bench=. -benchmem -benchtime=5s ./...

.PHONY: tests
tests: *_test.go
tests: *_test.go
$(GO_TEST) ./...

.PHONY: snake2camel
snake2camel:
@awk -i inplace '{ \
while ( match($$0, /(.*)([a-z]+[0-9]*)_([a-zA-Z0-9])(.*)/, cap) ) \
$$0 = cap[1] cap[2] toupper(cap[3]) cap[4]; \
print \
}' $(file)
3 changes: 3 additions & 0 deletions tools/snake2camel/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/leodido/go-urn/tools/snake2camel

go 1.21.6
Empty file added tools/snake2camel/go.sum
Empty file.
69 changes: 69 additions & 0 deletions tools/snake2camel/snake2camel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"os"
"regexp"
"strings"
)

func exitWithError(msg string) {
err := fmt.Sprintf("error: %s\n", msg)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) (string, int) {
result := ""
lastIndex := 0

for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
if v[i] == -1 || v[i+1] == -1 {
groups = append(groups, "")
} else {
groups = append(groups, str[v[i]:v[i+1]])
}
}

result += str[lastIndex:v[0]] + repl(groups)
lastIndex = v[1]
}

return result + str[lastIndex:], lastIndex
}

func snake2camel(content []byte) []byte {
re := regexp.MustCompile(`(.*)([a-z]+[0-9]*)_([a-zA-Z0-9])(.*)`)
res := string(content)
last := -1

for last != 0 {
res, last = replaceAllStringSubmatchFunc(re, res, func(groups []string) string {
return groups[1] + groups[2] + strings.ToUpper(groups[3]) + groups[4]
})
}

return []byte(res)
}

func snake2camelFile(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}
updated := snake2camel(content)

return os.WriteFile(path, updated, 0)
}

func main() {
if len(os.Args) != 2 {
exitWithError("must be called with the file path as the only argument")
}
path := os.Args[1]
if err := snake2camelFile(path); err != nil {
exitWithError(err.Error())
}
}
60 changes: 60 additions & 0 deletions tools/snake2camel/snake2camel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"testing"
)

func TestSnake2CamelNoReplace(t *testing.T) {
txt := `const start int = 1`

exp := `const start int = 1`

got := snake2camel([]byte(txt))
if string(got) != exp {
fmt.Printf("expected length: %d, got %d\n", len(exp), len(got))
t.Fatalf("expected:\n%s\ngot:\n%s", exp, got)
}
}

func TestSnake2CamelOneUnderscore(t *testing.T) {
txt := `const en_urn int = 5
const en_scim int = 44
const en_fail int = 81
const en_main int = 1`

exp := `const enUrn int = 5
const enScim int = 44
const enFail int = 81
const enMain int = 1`

got := snake2camel([]byte(txt))
if string(got) != exp {
fmt.Printf("expected length: %d, got %d\n", len(exp), len(got))
t.Fatalf("expected:\n%s\ngot:\n%s", exp, got)
}
}

func TestSnake2CamelMoreUnderscores(t *testing.T) {
txt := `if (m.p) == (m.pe) {
goto _test_eof
}
switch m.cs {
case 1:
goto st_case_1
}`

exp := `if (m.p) == (m.pe) {
goto _testEof
}
switch m.cs {
case 1:
goto stCase1
}`

got := snake2camel([]byte(txt))
if string(got) != exp {
fmt.Printf("expected length: %d, got %d\n", len(exp), len(got))
t.Fatalf("expected:\n%s\ngot:\n%s", exp, got)
}
}
6 changes: 3 additions & 3 deletions urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const errInvalidURN = "invalid URN: %s"
// Details at https://tools.ietf.org/html/rfc2141.
type URN struct {
prefix string // Static prefix. Equal to "urn" when empty.
ID string // Namespace identifier
SS string // Namespace specific string
ID string // Namespace identifier (NID)
SS string // Namespace specific string (NSS)
norm string // Normalized namespace specific string
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func (u URN) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}

// MarshalJSON unmarshals a URN from JSON string form (e.g. `"urn:oid:1.2.3.4"`).
// UnmarshalJSON unmarshals a URN from JSON string form (e.g. `"urn:oid:1.2.3.4"`).
func (u *URN) UnmarshalJSON(bytes []byte) error {
var str string
if err := json.Unmarshal(bytes, &str); err != nil {
Expand Down