Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
0d77947a
Commit
0d77947a
authored
Jun 23, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
publish strconv.UnquoteChar
R=r DELTA=69 (37 added, 3 deleted, 29 changed) OCL=30661 CL=30667
parent
a45c54d1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
27 deletions
+61
-27
src/pkg/strconv/quote.go
src/pkg/strconv/quote.go
+58
-24
src/pkg/strconv/quote_test.go
src/pkg/strconv/quote_test.go
+3
-3
No files found.
src/pkg/strconv/quote.go
View file @
0d77947a
...
...
@@ -97,22 +97,35 @@ func unhex(b byte) (v int, ok bool) {
return
;
}
func
unquoteChar
(
s
string
,
q
byte
)
(
t
,
ns
string
,
err
os
.
Error
)
{
err
=
os
.
EINVAL
;
// assume error for easy return
// UnquoteChar decodes the first character or byte in the escaped string
// or character literal represented by the string s.
// It returns four values:
// 1) value, the decoded Unicode code point or byte value;
// 2) multibyte, a boolean indicating whether the decoded character
// requires a multibyte UTF-8 representation;
// 3) tail, the remainder of the string after the character; and
// 4) an error that will be nil if the character is syntactically valid.
// The second argument, quote, specifies the type of literal being parsed
// and therefore which escaped quote character is permitted.
// If set to a single quote, it permits the sequence \' and disallows unescaped '.
// If set to a double quote, it permits \" and disallows unescaped ".
// If set to zero, it does not permit either escape and allows both quote characters to appear unescaped.
func
UnquoteChar
(
s
string
,
quote
byte
)
(
value
int
,
multibyte
bool
,
tail
string
,
err
os
.
Error
)
{
// easy cases
switch
c
:=
s
[
0
];
{
case
c
==
quote
&&
(
quote
==
'\'
'
||
quote
==
'"'
)
:
err
=
os
.
EINVAL
;
return
;
case
c
>=
utf8
.
RuneSelf
:
r
,
size
:=
utf8
.
DecodeRuneInString
(
s
);
return
s
[
0
:
size
],
s
[
size
:
len
(
s
)],
nil
;
case
c
==
q
:
return
;
return
r
,
true
,
s
[
size
:
len
(
s
)],
nil
;
case
c
!=
'\\'
:
return
s
[
0
:
1
]
,
s
[
1
:
len
(
s
)],
nil
;
return
int
(
s
[
0
]),
false
,
s
[
1
:
len
(
s
)],
nil
;
}
// hard case: c is backslash
if
len
(
s
)
<=
1
{
err
=
os
.
EINVAL
;
return
;
}
c
:=
s
[
1
];
...
...
@@ -120,19 +133,19 @@ func unquoteChar(s string, q byte) (t, ns string, err os.Error) {
switch
c
{
case
'a'
:
return
"
\a
"
,
s
,
nil
;
value
=
'\a'
;
case
'b'
:
return
"
\b
"
,
s
,
nil
;
value
=
'\b'
;
case
'f'
:
return
"
\f
"
,
s
,
nil
;
value
=
'\f'
;
case
'n'
:
return
"
\n
"
,
s
,
nil
;
value
=
'\n'
;
case
'r'
:
return
"
\r
"
,
s
,
nil
;
value
=
'\r'
;
case
't'
:
return
"
\t
"
,
s
,
nil
;
value
=
'\t'
;
case
'v'
:
return
"
\v
"
,
s
,
nil
;
value
=
'\v'
;
case
'x'
,
'u'
,
'U'
:
n
:=
0
;
switch
c
{
...
...
@@ -145,11 +158,13 @@ func unquoteChar(s string, q byte) (t, ns string, err os.Error) {
}
v
:=
0
;
if
len
(
s
)
<
n
{
err
=
os
.
EINVAL
;
return
;
}
for
j
:=
0
;
j
<
n
;
j
++
{
x
,
ok
:=
unhex
(
s
[
j
]);
if
!
ok
{
err
=
os
.
EINVAL
;
return
;
}
v
=
v
<<
4
|
x
;
...
...
@@ -157,15 +172,19 @@ func unquoteChar(s string, q byte) (t, ns string, err os.Error) {
s
=
s
[
n
:
len
(
s
)];
if
c
==
'x'
{
// single-byte string, possibly not UTF-8
return
string
([]
byte
{
byte
(
v
)}),
s
,
nil
;
value
=
v
;
break
;
}
if
v
>
utf8
.
RuneMax
{
err
=
os
.
EINVAL
;
return
;
}
return
string
(
v
),
s
,
nil
;
value
=
v
;
multibyte
=
true
;
case
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
:
v
:=
int
(
c
)
-
'0'
;
if
len
(
s
)
<
2
{
err
=
os
.
EINVAL
;
return
;
}
for
j
:=
0
;
j
<
2
;
j
++
{
// one digit already; two more
...
...
@@ -177,13 +196,23 @@ func unquoteChar(s string, q byte) (t, ns string, err os.Error) {
}
s
=
s
[
2
:
len
(
s
)];
if
v
>
255
{
err
=
os
.
EINVAL
;
return
;
}
return
string
(
v
),
s
,
nil
;
case
'\\'
,
q
:
return
string
(
c
),
s
,
nil
;
value
=
v
;
case
'\\'
:
value
=
'\\'
;
case
'\'
'
,
'"'
:
if
c
!=
quote
{
err
=
os
.
EINVAL
;
return
;
}
value
=
int
(
c
);
default
:
err
=
os
.
EINVAL
;
return
;
}
tail
=
s
;
return
;
}
...
...
@@ -212,14 +241,19 @@ func Unquote(s string) (t string, err os.Error) {
}
// TODO(rsc): String accumulation could be more efficient.
var
c
,
tt
string
;
var
err1
os
.
Error
;
var
tt
string
;
for
len
(
s
)
>
0
{
if
c
,
s
,
err1
=
unquoteChar
(
s
,
quote
);
err1
!=
nil
{
c
,
multibyte
,
ss
,
err1
:=
UnquoteChar
(
s
,
quote
);
if
err1
!=
nil
{
err
=
err1
;
return
;
}
tt
+=
c
;
s
=
ss
;
if
multibyte
||
c
<
utf8
.
RuneSelf
{
tt
+=
string
(
c
);
}
else
{
tt
+=
string
([]
byte
{
byte
(
c
)});
}
if
quote
==
'\'
'
&&
len
(
s
)
!=
0
{
// single-quoted must be single character
return
;
...
...
src/pkg/strconv/quote_test.go
View file @
0d77947a
...
...
@@ -149,7 +149,7 @@ func TestUnquote(t *testing.T) {
for
i
:=
0
;
i
<
len
(
unquotetests
);
i
++
{
tt
:=
unquotetests
[
i
];
if
out
,
err
:=
Unquote
(
tt
.
in
);
err
!=
nil
&&
out
!=
tt
.
out
{
t
.
Errorf
(
"Unquote(%
s) = %q, %s
want %q, nil"
,
tt
.
in
,
out
,
err
,
tt
.
out
);
t
.
Errorf
(
"Unquote(%
#q) = %q, %v
want %q, nil"
,
tt
.
in
,
out
,
err
,
tt
.
out
);
}
}
...
...
@@ -157,14 +157,14 @@ func TestUnquote(t *testing.T) {
for
i
:=
0
;
i
<
len
(
quotetests
);
i
++
{
tt
:=
quotetests
[
i
];
if
in
,
err
:=
Unquote
(
tt
.
out
);
in
!=
tt
.
in
{
t
.
Errorf
(
"Unquote(%
s) = %q, %s
, want %q, nil"
,
tt
.
out
,
in
,
err
,
tt
.
in
);
t
.
Errorf
(
"Unquote(%
#q) = %q, %v
, want %q, nil"
,
tt
.
out
,
in
,
err
,
tt
.
in
);
}
}
for
i
:=
0
;
i
<
len
(
misquoted
);
i
++
{
s
:=
misquoted
[
i
];
if
out
,
err
:=
Unquote
(
s
);
out
!=
""
||
err
!=
os
.
EINVAL
{
t
.
Errorf
(
"Unquote(%
q) = %q, %s want %q, %s
"
,
s
,
out
,
err
,
""
,
os
.
EINVAL
);
t
.
Errorf
(
"Unquote(%
#q) = %q, %v want %q, %v
"
,
s
,
out
,
err
,
""
,
os
.
EINVAL
);
}
}
}
Write
Preview
Markdown
is supported
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