/// <summary> /// Percent-encodes the string <paramref name="value"/> except for those characters for which <paramref name="isSafe"/> returns <c>true</c>. /// </summary> /// <param name="value">The value to encode.</param> /// <param name="isSafe">A function determining whether a character is safe (i.e., does not need encoding).</param> public static string PercentEncode(string value, Func <byte, bool> isSafe) { var bytes = Utf8EncodingWithoutBom.GetBytes(value); var sb = new StringBuilder(bytes.Length); foreach (var ch in bytes) { if (isSafe(ch)) { sb.Append((char)ch); } else { sb.Append(PercentEncode(ch)); } } return(sb.ToString()); }
/// <summary> /// Percent-encodes the string <paramref name="value"/> except for those characters for which <paramref name="isSafe"/> returns <c>true</c>. /// </summary> /// <param name="value">The value to encode.</param> /// <param name="isSafe">A function determining whether a character is safe (i.e., does not need encoding).</param> public static string PercentEncode(string value, Func <byte, bool> isSafe) { _ = value ?? throw new ArgumentNullException(nameof(value)); _ = isSafe ?? throw new ArgumentNullException(nameof(isSafe)); var bytes = Utf8EncodingWithoutBom.GetBytes(value); var sb = new StringBuilder(bytes.Length); foreach (var ch in bytes) { if (isSafe(ch)) { sb.Append((char)ch); } else { sb.Append(PercentEncode(ch)); } } return(sb.ToString()); }