Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
gopub
ptglob
Commits
87e123b5
Commit
87e123b5
authored
7 years ago
by
Carsten Orthbandt
Browse files
Options
Download
Email Patches
Plain Diff
Working
parent
ab1d78a9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.vscode/launch.json
+21
-0
.vscode/launch.json
.vscode/settings.json
+2
-0
.vscode/settings.json
ptglob.go
+63
-9
ptglob.go
ptglob_test.go
+29
-2
ptglob_test.go
with
115 additions
and
11 deletions
+115
-11
.vscode/launch.json
0 → 100644
+
21
-
0
View file @
87e123b5
{
//
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
This diff is collapsed.
Click to expand it.
.vscode/settings.json
0 → 100644
+
2
-
0
View file @
87e123b5
{
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
ptglob.go
+
63
-
9
View file @
87e123b5
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
}
This diff is collapsed.
Click to expand it.
ptglob_test.go
+
29
-
2
View file @
87e123b5
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."
)
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help