Commit 87e123b5 authored by Carsten Orthbandt's avatar Carsten Orthbandt
Browse files

Working

parent ab1d78a9
Showing with 115 additions and 11 deletions
+115 -11
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
]
}
\ No newline at end of file
{
}
\ No newline at end of file
package ptglob
func Match(text, pattern string) bool {
func Match(text, pattern string) (bool, []string) {
matches := make([]string, 0)
if pattern == "" {
if text == "" {
return true
return true, matches
}
return false
return false, make([]string, 0)
}
if pattern == "*" {
return true
return true, matches
}
txtpos := 0
patpos := 0
......@@ -17,31 +18,84 @@ func Match(text, pattern string) bool {
cp := -1
mp := -1
for (txtpos < txtlen) && (pattern[patpos] != '*') {
if (text[txtpos] != pattern[patpos]) && (pattern[patpos] != '?') {
return false
if text[txtpos] != pattern[patpos] {
if pattern[patpos] != '?' {
return false, make([]string, 0)
}
matches = append(matches, text[txtpos:txtpos+1])
}
txtpos++
patpos++
}
cur := ""
for txtpos < txtlen {
if pattern[patpos] == '*' {
if len(cur) > 0 {
matches = append(matches, cur)
cur = ""
}
patpos++
if patpos >= patlen {
return true
if txtpos < txtlen {
matches = append(matches, text[txtpos:txtlen])
}
return true, matches
}
mp = patpos
cp = txtpos + 1
} else if (patpos < patlen) && ((text[txtpos] == pattern[patpos]) || (pattern[patpos] == '?')) {
if len(cur) > 0 {
matches = append(matches, cur)
cur = ""
}
if text[txtpos] != pattern[patpos] {
matches = append(matches, text[txtpos:txtpos+1])
}
patpos++
txtpos++
} else {
cur = cur + text[txtpos:txtpos+1]
patpos = mp
txtpos = cp
cp++
}
}
if len(cur) > 0 {
matches = append(matches, cur)
cur = ""
}
if patpos < patlen {
return false
return false, make([]string, 0)
}
return true
return true, matches
}
func Rewrite(text, pattern, rewrite string) (bool, string) {
ok, m := Match(text, pattern)
if !ok {
return false, ""
}
r := ""
l := len(rewrite)
for p := 0; p < l; p++ {
c := rewrite[p]
if c == '%' {
p++
if p < l {
c = rewrite[p]
if c == '%' {
r = r + "%"
} else {
idx := int(c - '1')
if idx >= 0 && idx < len(m) {
r = r + m[idx]
}
}
}
} else {
r = r + string(c)
}
}
return true, r
}
package ptglob
import (
"log"
"testing"
)
func testM1(t *testing.T, text, pattern string, want bool) {
res := Match(text, pattern)
res, matches := Match(text, pattern)
log.Printf("M('%v','%v') => %v %v", text, pattern, res, matches)
if want != res {
t.Fatalf("e: '%v' <-> '%v' %v/%v", text, pattern, want, res)
t.Errorf("Match('%v','%v') should return %v but got %v", text, pattern, want, res)
}
}
func testR1(t *testing.T, text, pattern, rewrite, want string) {
ok, res := Rewrite(text, pattern, rewrite)
log.Printf("R('%v','%v','%v') => %v %v", text, pattern, rewrite, ok, res)
if want == "" {
if ok {
t.Errorf("Rewrite('%v','%v','%v') should return false but got true", text, pattern, rewrite)
}
} else {
if !ok {
t.Errorf("Rewrite('%v','%v','%v') should return true but got false", text, pattern, rewrite)
} else {
if res != want {
t.Errorf("Rewrite('%v','%v','%v') should return '%v' but got '%v'", text, pattern, rewrite, want, res)
}
}
}
}
func TestPTGlob(t *testing.T) {
testM1(t, "ab", "*b", true)
testM1(t, "ab", "a*", true)
testM1(t, "", "", true)
testM1(t, "x", "", false)
testM1(t, "", "x", false)
......@@ -25,4 +47,9 @@ func TestPTGlob(t *testing.T) {
testM1(t, "abcdef", "*ab*ef", true)
testM1(t, "abcdef", "*ab?ef", false)
testM1(t, "abcdef", "*ab??ef", true)
testM1(t, "abcdef", "*c*e*", true)
testM1(t, "abcdefg", "*c*e*g", true)
testR1(t, "abcdefg", "ab*e*g", "x%1", "xcd")
testR1(t, "abcdefg", "ab*e*g", "%%1", "%1")
testR1(t, "abcdefg", "ab*e*g", ".%1.%1.%2.", ".cd.cd.f.")
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment