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

Commit c86361f

Browse files
committed
Add string.ParseKeyValueText and string.ReadLines extension methods
1 parent 4478e32 commit c86361f

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
using System;
1414
using System.Collections.Generic;
1515
using System.Globalization;
16+
using System.IO;
17+
using System.Linq;
1618
using System.Text;
1719
using System.Text.RegularExpressions;
1820
using ServiceStack.Text;
@@ -889,6 +891,31 @@ public static byte[] ToAsciiBytes(this string value)
889891
return PclExport.Instance.GetAsciiBytes(value);
890892
}
891893

894+
public static Dictionary<string,string> ParseKeyValueText(this string text, string delimiter=":")
895+
{
896+
var to = new Dictionary<string, string>();
897+
if (text == null) return to;
898+
899+
foreach (var parts in text.ReadLines().Select(line => line.SplitOnFirst(delimiter)))
900+
{
901+
var key = parts[0].Trim();
902+
if (key.Length == 0) continue;
903+
to[key] = parts.Length == 2 ? parts[1].Trim() : null;
904+
}
905+
906+
return to;
907+
}
908+
909+
public static IEnumerable<string> ReadLines(this string text)
910+
{
911+
string line;
912+
var reader = new StringReader(text ?? "");
913+
while ((line = reader.ReadLine()) != null)
914+
{
915+
yield return line;
916+
}
917+
}
918+
892919
#if !XBOX
893920
public static string HexEscape(this string text, params char[] anyCharOf)
894921
{

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using NUnit.Framework;
45

56
namespace ServiceStack.Text.Tests
@@ -196,5 +197,30 @@ public void Can_trim_prefixes()
196197
Assert.That("/path/info".TrimPrefixes("/www_deploy", "~/www_deploy"),
197198
Is.EqualTo("/path/info"));
198199
}
200+
201+
[Test]
202+
public void Can_read_lines()
203+
{
204+
Assert.That((null as string).ReadLines().Count(), Is.EqualTo(0));
205+
Assert.That("".ReadLines().Count(), Is.EqualTo(0));
206+
Assert.That("a".ReadLines().Count(), Is.EqualTo(1));
207+
Assert.That("a\nb".ReadLines().Count(), Is.EqualTo(2));
208+
Assert.That("a\r\nb".ReadLines().Count(), Is.EqualTo(2));
209+
}
210+
211+
[Test]
212+
public void Can_ParseKeyValueText()
213+
{
214+
Assert.That("".ParseKeyValueText().Count, Is.EqualTo(0));
215+
Assert.That("a".ParseKeyValueText().Count, Is.EqualTo(1));
216+
Assert.That("a".ParseKeyValueText()["a"], Is.Null);
217+
Assert.That("a:".ParseKeyValueText().Count, Is.EqualTo(1));
218+
Assert.That("a:".ParseKeyValueText()["a"], Is.EqualTo(""));
219+
Assert.That("a:b".ParseKeyValueText()["a"], Is.EqualTo("b"));
220+
Assert.That("a:b:c".ParseKeyValueText()["a"], Is.EqualTo("b:c"));
221+
Assert.That("a : b:c ".ParseKeyValueText()["a"], Is.EqualTo("b:c"));
222+
Assert.That("a:b\nc:d".ParseKeyValueText()["c"], Is.EqualTo("d"));
223+
Assert.That("a:b\r\nc:d".ParseKeyValueText()["c"], Is.EqualTo("d"));
224+
}
199225
}
200226
}

0 commit comments

Comments
 (0)