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
5ce95b4b
Commit
5ce95b4b
authored
Aug 12, 2011
by
csharptest
Committed by
rogerk
Aug 12, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tests for the additions to NameHelpers.UnderscoresToXxxxxCase
parent
1851676b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
21 deletions
+39
-21
NameHelpersTest.cs
src/ProtocolBuffers.Test/NameHelpersTest.cs
+10
-0
NameHelpers.cs
src/ProtocolBuffers/NameHelpers.cs
+29
-21
No files found.
src/ProtocolBuffers.Test/NameHelpersTest.cs
View file @
5ce95b4b
...
@@ -48,6 +48,11 @@ namespace Google.ProtocolBuffers
...
@@ -48,6 +48,11 @@ namespace Google.ProtocolBuffers
Assert
.
AreEqual
(
"FooBar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"foo_bar"
));
Assert
.
AreEqual
(
"FooBar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"foo_bar"
));
Assert
.
AreEqual
(
"Foo0Bar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"Foo0bar"
));
Assert
.
AreEqual
(
"Foo0Bar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"Foo0bar"
));
Assert
.
AreEqual
(
"FooBar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"Foo_+_Bar"
));
Assert
.
AreEqual
(
"FooBar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"Foo_+_Bar"
));
Assert
.
AreEqual
(
"Bar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"__+bar"
));
Assert
.
AreEqual
(
"Bar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"bar_"
));
Assert
.
AreEqual
(
"_0Bar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"_0bar"
));
Assert
.
AreEqual
(
"_1Bar"
,
NameHelpers
.
UnderscoresToPascalCase
(
"_1_bar"
));
}
}
[
Test
]
[
Test
]
...
@@ -57,6 +62,11 @@ namespace Google.ProtocolBuffers
...
@@ -57,6 +62,11 @@ namespace Google.ProtocolBuffers
Assert
.
AreEqual
(
"fooBar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"foo_bar"
));
Assert
.
AreEqual
(
"fooBar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"foo_bar"
));
Assert
.
AreEqual
(
"foo0Bar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"Foo0bar"
));
Assert
.
AreEqual
(
"foo0Bar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"Foo0bar"
));
Assert
.
AreEqual
(
"fooBar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"Foo_+_Bar"
));
Assert
.
AreEqual
(
"fooBar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"Foo_+_Bar"
));
Assert
.
AreEqual
(
"bar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"__+bar"
));
Assert
.
AreEqual
(
"bar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"bar_"
));
Assert
.
AreEqual
(
"_0Bar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"_0bar"
));
Assert
.
AreEqual
(
"_1Bar"
,
NameHelpers
.
UnderscoresToCamelCase
(
"_1_bar"
));
}
}
[
Test
]
[
Test
]
...
...
src/ProtocolBuffers/NameHelpers.cs
View file @
5ce95b4b
...
@@ -48,37 +48,45 @@ namespace Google.ProtocolBuffers
...
@@ -48,37 +48,45 @@ namespace Google.ProtocolBuffers
{
{
public
static
string
UnderscoresToPascalCase
(
string
input
)
public
static
string
UnderscoresToPascalCase
(
string
input
)
{
{
return
UnderscoresToPascalOrCamelCase
(
input
,
true
);
string
name
=
UnderscoresToUpperCase
(
input
);
// Pascal case always begins with upper-case letter
if
(
Char
.
IsLower
(
name
[
0
]))
{
char
[]
chars
=
name
.
ToCharArray
();
chars
[
0
]
=
char
.
ToUpper
(
chars
[
0
]);
return
new
string
(
chars
);
}
return
name
;
}
}
public
static
string
UnderscoresToCamelCase
(
string
input
)
public
static
string
UnderscoresToCamelCase
(
string
input
)
{
{
return
UnderscoresToPascalOrCamelCase
(
input
,
false
);
string
name
=
UnderscoresToUpperCase
(
input
);
// Camel case always begins with lower-case letter
if
(
Char
.
IsUpper
(
name
[
0
]))
{
char
[]
chars
=
name
.
ToCharArray
();
chars
[
0
]
=
char
.
ToLower
(
chars
[
0
]);
return
new
string
(
chars
);
}
return
name
;
}
}
/// <summary>
/// <summary>
/// Converts a string to Pascal or Camel case. The first letter is capitalized or
/// Capitalizes any characters following an '_' or a number '0' - '9' and removes
/// lower-cased depending on <paramref name="pascal"/> is true.
/// all non alpha-numberic characters. If the resulting string begins with a number
/// After the first letter, any punctuation is removed but triggers capitalization
/// an '_' will be prefixed.
/// of the next letter. Digits are preserved but trigger capitalization of the next
/// letter.
/// All capitalisation is done in the invariant culture.
/// </summary>
/// </summary>
private
static
string
UnderscoresTo
PascalOrCamelCase
(
string
input
,
bool
pascal
)
private
static
string
UnderscoresTo
UpperCase
(
string
input
)
{
{
string
name
=
Transform
(
input
,
pascal
?
UnderlineToPascal
:
UnderlineToCamel
,
x
=>
x
.
Value
.
TrimStart
(
'_'
).
ToUpper
());
string
name
=
Transform
(
input
,
UnderlineCharacter
,
x
=>
x
.
Value
.
ToUpper
());
name
=
Transform
(
name
,
InvalidCharacters
,
x
=>
String
.
Empty
);
if
(
name
.
Length
==
0
)
if
(
name
.
Length
==
0
)
throw
new
ArgumentException
(
String
.
Format
(
"The field name '{0}' is invalid."
,
input
));
throw
new
ArgumentException
(
String
.
Format
(
"The field name '{0}' is invalid."
,
input
));
// Pascal case always begins with lower-case letter
if
(!
pascal
&&
Char
.
IsUpper
(
name
[
0
]))
{
char
[]
chars
=
name
.
ToCharArray
();
chars
[
0
]
=
char
.
ToLower
(
chars
[
0
]);
return
new
string
(
chars
);
}
// Fields can not start with a number
// Fields can not start with a number
if
(
Char
.
IsNumber
(
name
[
0
]))
if
(
Char
.
IsNumber
(
name
[
0
]))
name
=
'_'
+
name
;
name
=
'_'
+
name
;
...
@@ -110,14 +118,14 @@ namespace Google.ProtocolBuffers
...
@@ -110,14 +118,14 @@ namespace Google.ProtocolBuffers
}
}
/// <summary>
/// <summary>
///
Similar to UnderlineToCamel, but also matches the first character if it is lower-case
///
All characters that are not alpha-numberic
/// </summary>
/// </summary>
private
static
Regex
UnderlineToPascal
=
new
Regex
(
@"(?:(?:^|[0-9_])[a-z])|_
"
);
private
static
Regex
InvalidCharacters
=
new
Regex
(
@"[^a-zA-Z0-9]+
"
);
/// <summary>
/// <summary>
/// Matches lower-case character that follow either an underscore, or a number
/// Matches lower-case character that follow either an underscore, or a number
/// </summary>
/// </summary>
private
static
Regex
Underline
ToCamel
=
new
Regex
(
@"(?:[0-9_][a-z])|_
"
);
private
static
Regex
Underline
Character
=
new
Regex
(
@"[0-9_][a-z]
"
);
/// <summary>
/// <summary>
/// Used for text-template transformation where a regex match is replaced in the input string.
/// Used for text-template transformation where a regex match is replaced in the input string.
...
...
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