From a8ffc60d317889f8e733a64437e807558b06cb0e Mon Sep 17 00:00:00 2001 From: Licshee Date: Sat, 6 Feb 2016 15:03:52 +0800 Subject: [PATCH] updated StringEx to 0.2, in favor of s.NonWhiteSpaceLines() --- shadowsocks-csharp/Controller/I18N.cs | 5 +-- .../Controller/Service/GfwListUpdater.cs | 6 +-- .../Controller/ShadowsocksController.cs | 3 +- shadowsocks-csharp/StringEx.cs | 41 +++++++++++++++++++ shadowsocks-csharp/packages.config | 2 +- 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/shadowsocks-csharp/Controller/I18N.cs b/shadowsocks-csharp/Controller/I18N.cs index fcae64da..85f3ae60 100755 --- a/shadowsocks-csharp/Controller/I18N.cs +++ b/shadowsocks-csharp/Controller/I18N.cs @@ -18,10 +18,9 @@ namespace Shadowsocks.Controller { using (var sr = new StringReader(Resources.cn)) { - string line; - while ((line = sr.ReadLine()) != null) + foreach (var line in sr.NonWhiteSpaceLines()) { - if (line.BeginWith('#')) + if (line[0] == '#') continue; var pos = line.IndexOf('='); diff --git a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs index 8098897c..3d04fafc 100644 --- a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs +++ b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs @@ -42,8 +42,7 @@ namespace Shadowsocks.Controller string local = File.ReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); using (var sr = new StringReader(local)) { - string rule; - while ((rule = sr.ReadLine()) != null) + foreach (var rule in sr.NonWhiteSpaceLines()) { if (rule.BeginWithAny(IgnoredLineBegins)) continue; @@ -100,8 +99,7 @@ namespace Shadowsocks.Controller List valid_lines = new List(); using (var sr = new StringReader(content)) { - string line; - while ((line = sr.ReadLine()) != null) + foreach (var line in sr.NonWhiteSpaceLines()) { if (line.BeginWithAny(IgnoredLineBegins)) continue; diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index d8a76ced..4b86a204 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -457,8 +457,7 @@ namespace Shadowsocks.Controller string local = File.ReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); using (var sr = new StringReader(local)) { - string rule; - while ((rule = sr.ReadLine()) != null) + foreach (var rule in sr.NonWhiteSpaceLines()) { if (rule.BeginWithAny(IgnoredLineBegins)) continue; diff --git a/shadowsocks-csharp/StringEx.cs b/shadowsocks-csharp/StringEx.cs index e116ed73..5f107a50 100644 --- a/shadowsocks-csharp/StringEx.cs +++ b/shadowsocks-csharp/StringEx.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Text; @@ -26,6 +27,17 @@ static partial class StringEx public static bool IsNullOrWhiteSpace(this string value) => string.IsNullOrWhiteSpace(value); + public static bool IsWhiteSpace(this string value) + { + foreach(var c in value) + { + if (char.IsWhiteSpace(c)) continue; + + return false; + } + return true; + } + #if !PCL public static string IsInterned(this string value) { @@ -135,6 +147,35 @@ static partial class StringEx #endregion + #region ToLines + + public static IEnumerable ToLines(this TextReader reader) + { + string line; + while ((line = reader.ReadLine()) != null) + yield return line; + } + public static IEnumerable NonEmptyLines(this TextReader reader) + { + string line; + while ((line = reader.ReadLine()) != null) + { + if (line == "") continue; + yield return line; + } + } + public static IEnumerable NonWhiteSpaceLines(this TextReader reader) + { + string line; + while ((line = reader.ReadLine()) != null) + { + if (line.IsWhiteSpace()) continue; + yield return line; + } + } + + #endregion + #region others private static readonly char[][] Quotes = new[] diff --git a/shadowsocks-csharp/packages.config b/shadowsocks-csharp/packages.config index 2dfd52b7..93252521 100644 --- a/shadowsocks-csharp/packages.config +++ b/shadowsocks-csharp/packages.config @@ -8,6 +8,6 @@ - + \ No newline at end of file