Skip to content

Commit

Permalink
Reintroduce Tavis.UriTemplates which had been upgraded to netstandard…
Browse files Browse the repository at this point in the history
… 2.1 in 2022.
  • Loading branch information
zijianhuang committed Jun 20, 2024
1 parent 418c91a commit 4c6c650
Show file tree
Hide file tree
Showing 8 changed files with 898 additions and 894 deletions.
4 changes: 4 additions & 0 deletions Fonlow.Web.MetaCore/Fonlow.Web.MetaCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Tavis.UriTemplates" Version="2.0.0" />
</ItemGroup>

</Project>
24 changes: 12 additions & 12 deletions Fonlow.Web.MetaCore/UriTemplatesCore/OperatorInfo.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace Tavis.UriTemplates
{
public class OperatorInfo
{
public bool Default { get; set; }
public string First { get; set; }
public char Separator { get; set; }
public bool Named { get; set; }
public string IfEmpty { get; set; }
public bool AllowReserved { get; set; }
//namespace Tavis.UriTemplates
//{
// public class OperatorInfo
// {
// public bool Default { get; set; }
// public string First { get; set; }
// public char Separator { get; set; }
// public bool Named { get; set; }
// public string IfEmpty { get; set; }
// public bool AllowReserved { get; set; }

}
}
// }
//}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Tavis.UriTemplates
{
public enum QueryStringParameterOrder
{
Strict,
Any
}
}
//namespace Tavis.UriTemplates
//{
// public enum QueryStringParameterOrder
// {
// Strict,
// Any
// }
//}
294 changes: 147 additions & 147 deletions Fonlow.Web.MetaCore/UriTemplatesCore/Result.cs
Original file line number Diff line number Diff line change
@@ -1,149 +1,149 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tavis.UriTemplates
{
public class Result
{
public bool ErrorDetected { get; set; }
public IList<string> ParameterNames {get;}
private const string _UriReservedSymbols = ":/?#[]@!$&'()*+,;=";
private const string _UriUnreservedSymbols = "-._~";

private StringBuilder _Result = new StringBuilder();

public Result()
{
ParameterNames = new List<string>();
}
public StringBuilder Append(char value)
{
return _Result.Append(value);
}
public StringBuilder Append(string value)
{

return _Result.Append(value);
}

public override string ToString()
{
return _Result.ToString();
}
public void AppendName(string variable, OperatorInfo op, bool valueIsEmpty)
{
_Result.Append(variable);
if (valueIsEmpty) { _Result.Append(op.IfEmpty); } else { _Result.Append('='); }
}


public void AppendList(OperatorInfo op, bool explode, string variable, IList list)
{
foreach (object item in list)
{
if (op.Named && explode)
{
_Result.Append(variable);
_Result.Append('=');
}
AppendValue(item.ToString(), 0, op.AllowReserved);

_Result.Append(explode ? op.Separator : ',');
}
if (list.Count > 0)
{
_Result.Remove(_Result.Length - 1, 1);
}
}

public void AppendDictionary(OperatorInfo op, bool explode, IDictionary<string, string> dictionary)
{
foreach (string key in dictionary.Keys)
{
_Result.Append(Encode(key, op.AllowReserved));
if (explode) _Result.Append('='); else _Result.Append(',');
AppendValue(dictionary[key], 0, op.AllowReserved);

if (explode)
{
_Result.Append(op.Separator);
}
else
{
_Result.Append(',');
}
}
if (dictionary.Count > 0)
{
_Result.Remove(_Result.Length - 1, 1);
}
}

public void AppendValue(string value, int prefixLength, bool allowReserved)
{

if (prefixLength != 0)
{
if (prefixLength < value.Length)
{
value = value.Substring(0, prefixLength);
}
}

_Result.Append(Encode(value, allowReserved));

}
//using System.Collections;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace Tavis.UriTemplates
//{
// public class Result
// {
// public bool ErrorDetected { get; set; }
// public IList<string> ParameterNames {get;}
// private const string _UriReservedSymbols = ":/?#[]@!$&'()*+,;=";
// private const string _UriUnreservedSymbols = "-._~";

// private StringBuilder _Result = new StringBuilder();

// public Result()
// {
// ParameterNames = new List<string>();
// }
// public StringBuilder Append(char value)
// {
// return _Result.Append(value);
// }
// public StringBuilder Append(string value)
// {

// return _Result.Append(value);
// }

// public override string ToString()
// {
// return _Result.ToString();
// }
// public void AppendName(string variable, OperatorInfo op, bool valueIsEmpty)
// {
// _Result.Append(variable);
// if (valueIsEmpty) { _Result.Append(op.IfEmpty); } else { _Result.Append('='); }
// }


// public void AppendList(OperatorInfo op, bool explode, string variable, IList list)
// {
// foreach (object item in list)
// {
// if (op.Named && explode)
// {
// _Result.Append(variable);
// _Result.Append('=');
// }
// AppendValue(item.ToString(), 0, op.AllowReserved);

// _Result.Append(explode ? op.Separator : ',');
// }
// if (list.Count > 0)
// {
// _Result.Remove(_Result.Length - 1, 1);
// }
// }

// public void AppendDictionary(OperatorInfo op, bool explode, IDictionary<string, string> dictionary)
// {
// foreach (string key in dictionary.Keys)
// {
// _Result.Append(Encode(key, op.AllowReserved));
// if (explode) _Result.Append('='); else _Result.Append(',');
// AppendValue(dictionary[key], 0, op.AllowReserved);

// if (explode)
// {
// _Result.Append(op.Separator);
// }
// else
// {
// _Result.Append(',');
// }
// }
// if (dictionary.Count > 0)
// {
// _Result.Remove(_Result.Length - 1, 1);
// }
// }

// public void AppendValue(string value, int prefixLength, bool allowReserved)
// {

// if (prefixLength != 0)
// {
// if (prefixLength < value.Length)
// {
// value = value.Substring(0, prefixLength);
// }
// }

// _Result.Append(Encode(value, allowReserved));

// }


private static string Encode(string p, bool allowReserved)
{

StringBuilder result = new StringBuilder();
foreach (char c in p)
{
if ((c >= 'A' && c <= 'z') //Alpha
|| (c >= '0' && c <= '9') // Digit
|| _UriUnreservedSymbols.Contains(c) // Unreserved symbols - These should never be percent encoded
|| (allowReserved && _UriReservedSymbols.Contains(c))) // Reserved symbols - should be included if requested (+)
{
result.Append(c);
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(new []{c});
foreach (byte abyte in bytes)
{
result.Append(HexEscape(abyte));
}

}
}

return result.ToString();


}

public static string HexEscape(byte i)
{
char[] esc = new char[3];
esc[0] = '%';
esc[1] = HexDigits[((i & 240) >> 4)];
esc[2] = HexDigits[(i & 15)];
return new string(esc);
}
public static string HexEscape(char c) {
char[] esc = new char[3];
esc[0] = '%';
esc[1] = HexDigits[(((int) c & 240) >> 4)];
esc[2] = HexDigits[((int) c & 15)];
return new string(esc);
}
private static readonly char[] HexDigits = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};



}
}
// private static string Encode(string p, bool allowReserved)
// {

// StringBuilder result = new StringBuilder();
// foreach (char c in p)
// {
// if ((c >= 'A' && c <= 'z') //Alpha
// || (c >= '0' && c <= '9') // Digit
// || _UriUnreservedSymbols.Contains(c) // Unreserved symbols - These should never be percent encoded
// || (allowReserved && _UriReservedSymbols.Contains(c))) // Reserved symbols - should be included if requested (+)
// {
// result.Append(c);
// }
// else
// {
// byte[] bytes = Encoding.UTF8.GetBytes(new []{c});
// foreach (byte abyte in bytes)
// {
// result.Append(HexEscape(abyte));
// }

// }
// }

// return result.ToString();


// }

// public static string HexEscape(byte i)
// {
// char[] esc = new char[3];
// esc[0] = '%';
// esc[1] = HexDigits[((i & 240) >> 4)];
// esc[2] = HexDigits[(i & 15)];
// return new string(esc);
// }
// public static string HexEscape(char c) {
// char[] esc = new char[3];
// esc[0] = '%';
// esc[1] = HexDigits[(((int) c & 240) >> 4)];
// esc[2] = HexDigits[((int) c & 15)];
// return new string(esc);
// }
// private static readonly char[] HexDigits = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};



// }
//}
Loading

0 comments on commit 4c6c650

Please sign in to comment.