Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 302a60b

Browse files
committed
Add option to urlencode in upper case
1 parent 62b8f40 commit 302a60b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ public static string DecodeJsv(this string value)
134134
.Replace(TypeSerializer.DoubleQuoteString, JsWriter.QuoteString);
135135
}
136136

137-
public static string UrlEncode(this string text)
137+
public static string UrlEncode(this string text, bool upperCase=false)
138138
{
139139
if (String.IsNullOrEmpty(text)) return text;
140140

141141
var sb = new StringBuilder();
142+
var fmt = upperCase ? "X2" : "x2";
142143

143144
foreach (var charCode in Encoding.UTF8.GetBytes(text))
144145
{
@@ -158,7 +159,7 @@ public static string UrlEncode(this string text)
158159
}
159160
else
160161
{
161-
sb.Append('%' + charCode.ToString("x2"));
162+
sb.Append('%' + charCode.ToString(fmt));
162163
}
163164
}
164165

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ public void Can_Url_Encode_String()
127127
Assert.That(decoded, Is.EqualTo(text));
128128
}
129129

130+
[Test]
131+
public void Can_UrlEncode_in_upper_case()
132+
{
133+
var chars = "/=";
134+
Assert.That(chars.UrlEncode(), Is.EqualTo("%2f%3d"));
135+
Assert.That(chars.UrlEncode(upperCase:true), Is.EqualTo("%2F%3D"));
136+
}
137+
130138
[Test]
131139
public void Can_ToCamelCase_String()
132140
{

0 commit comments

Comments
 (0)