replace usage of string.Split(char[], int), not supported in full PCL profile (#119)
This commit is contained in:
parent
840a690f13
commit
888a1a3fbd
@ -101,5 +101,14 @@ namespace Flurl.Test
|
||||
};
|
||||
Assert.Throws<ArgumentException>(() => kv.ToKeyValuePairs().ToList());// need to force it to iterate for the exception to be thrown
|
||||
}
|
||||
|
||||
// todo: why can't netcore test app find SplitOnFirstOccurence? (making it public doesn't help)
|
||||
#if !NETCOREAPP1_0
|
||||
[Test]
|
||||
public void SplitOnFirstOccurence_works() {
|
||||
var result = "hello/how/are/you".SplitOnFirstOccurence('/');
|
||||
Assert.AreEqual(new[] { "hello", "how/are/you" }, result);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -42,9 +42,9 @@ namespace Flurl
|
||||
if (baseUrl == null)
|
||||
throw new ArgumentNullException(nameof(baseUrl));
|
||||
|
||||
var parts = baseUrl.Split(new[] { '#' }, 2);
|
||||
var parts = baseUrl.SplitOnFirstOccurence('#');
|
||||
Fragment = (parts.Length == 2) ? parts[1] : "";
|
||||
parts = parts[0].Split(new[] { '?' }, 2);
|
||||
parts = parts[0].SplitOnFirstOccurence('?');
|
||||
Query = (parts.Length == 2) ? parts[1] : "";
|
||||
Path = parts[0];
|
||||
}
|
||||
@ -62,7 +62,7 @@ namespace Flurl
|
||||
|
||||
result.AddRange(
|
||||
from p in query.Split('&')
|
||||
let pair = p.Split(new[] { '=' }, 2)
|
||||
let pair = p.SplitOnFirstOccurence('=')
|
||||
let name = pair[0]
|
||||
let value = (pair.Length == 1) ? "" : pair[1]
|
||||
select new QueryParameter(name, value, true));
|
||||
|
@ -48,6 +48,20 @@ namespace Flurl.Util
|
||||
return obj.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Needed because full PCL profile doesn't support Split(char[], int) (#119)
|
||||
/// </summary>
|
||||
internal static string[] SplitOnFirstOccurence(this string s, char separator) {
|
||||
if (string.IsNullOrEmpty(s))
|
||||
return new[] { s };
|
||||
|
||||
var i = s.IndexOf(separator);
|
||||
if (i == -1)
|
||||
return new[] { s };
|
||||
|
||||
return new[] { s.Substring(0, i), s.Substring(i + 1) };
|
||||
}
|
||||
|
||||
private static IEnumerable<KeyValuePair<string, object>> StringToKV(string s) {
|
||||
return Url.ParseQueryParams(s).Select(p => new KeyValuePair<string, object>(p.Name, p.Value));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user