Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
90f2f502
Commit
90f2f502
authored
Nov 26, 2014
by
Feng Xiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make Java TextFormat.printToUnicodeString() escape newlines.
Fix issue #77
parent
209840ec
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
6 deletions
+15
-6
TextFormat.java
java/src/main/java/com/google/protobuf/TextFormat.java
+2
-1
TextFormatTest.java
java/src/test/java/com/google/protobuf/TextFormatTest.java
+13
-5
No files found.
java/src/main/java/com/google/protobuf/TextFormat.java
View file @
90f2f502
...
...
@@ -411,7 +411,8 @@ public final class TextFormat {
generator
.
print
(
"\""
);
generator
.
print
(
escapeNonAscii
?
escapeText
((
String
)
value
)
:
escapeDoubleQuotesAndBackslashes
((
String
)
value
));
escapeDoubleQuotesAndBackslashes
((
String
)
value
)
.
replace
(
"\n"
,
"\\n"
));
generator
.
print
(
"\""
);
break
;
...
...
java/src/test/java/com/google/protobuf/TextFormatTest.java
View file @
90f2f502
...
...
@@ -864,15 +864,15 @@ public class TextFormatTest extends TestCase {
assertEquals
(
message
.
getOptionalString
(),
builder
.
getOptionalString
());
}
public
void
testPrintToUnicodeStringWithNewlines
()
{
public
void
testPrintToUnicodeStringWithNewlines
()
throws
Exception
{
// No newlines at start and end
assertEquals
(
"optional_string: \"test newlines\
n\nin
\nstring\"\n"
,
assertEquals
(
"optional_string: \"test newlines\
\n\\nin\
\nstring\"\n"
,
TextFormat
.
printToUnicodeString
(
TestAllTypes
.
newBuilder
()
.
setOptionalString
(
"test newlines\n\nin\nstring"
)
.
build
()));
// Newlines at start and end
assertEquals
(
"optional_string: \"\
ntest\nnewlines\n\nin\nstring
\n\"\n"
,
assertEquals
(
"optional_string: \"\
\ntest\\nnewlines\\n\\nin\\nstring\
\n\"\n"
,
TextFormat
.
printToUnicodeString
(
TestAllTypes
.
newBuilder
()
.
setOptionalString
(
"\ntest\nnewlines\n\nin\nstring\n"
)
.
build
()));
...
...
@@ -882,14 +882,22 @@ public class TextFormatTest extends TestCase {
TextFormat
.
printToUnicodeString
(
TestAllTypes
.
newBuilder
()
.
setOptionalString
(
""
)
.
build
()));
assertEquals
(
"optional_string: \"\n\"\n"
,
assertEquals
(
"optional_string: \"\
\
n\"\n"
,
TextFormat
.
printToUnicodeString
(
TestAllTypes
.
newBuilder
()
.
setOptionalString
(
"\n"
)
.
build
()));
assertEquals
(
"optional_string: \"\
n
\n\"\n"
,
assertEquals
(
"optional_string: \"\
\n\
\n\"\n"
,
TextFormat
.
printToUnicodeString
(
TestAllTypes
.
newBuilder
()
.
setOptionalString
(
"\n\n"
)
.
build
()));
// Test escaping roundtrip
TestAllTypes
message
=
TestAllTypes
.
newBuilder
()
.
setOptionalString
(
"\ntest\nnewlines\n\nin\nstring\n"
)
.
build
();
TestAllTypes
.
Builder
builder
=
TestAllTypes
.
newBuilder
();
TextFormat
.
merge
(
TextFormat
.
printToUnicodeString
(
message
),
builder
);
assertEquals
(
message
.
getOptionalString
(),
builder
.
getOptionalString
());
}
public
void
testPrintToUnicodeString_unknown
()
{
...
...
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