Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CustomEncode.cs
2using System.Text;
3using Waher.Content;
10
12{
17 {
26 public CustomEncode(ScriptNode Binary, ScriptNode ContentType, int Start, int Length, Expression Expression)
27 : base(Binary, ContentType, Start, Length, Expression)
28 {
29 }
30
34 public override string FunctionName => nameof(CustomEncode);
35
39 public override string[] DefaultArgumentNames => new string[] { "Binary", "ContentType" };
40
49 {
50 object Obj = Argument1.AssociatedObjectValue;
51 string ContentType = ToString(Argument2) ?? string.Empty;
52
53 if (!(Obj is byte[] Bin))
54 {
55 if (Obj is string s)
56 {
57 KeyValuePair<string, string>[] Fields = CommonTypes.ParseFieldValues(ContentType.Trim());
58 string Charset = null;
59
60 foreach (KeyValuePair<string, string> P in Fields)
61 {
62 if (string.Compare(P.Key, "charset", true) == 0)
63 {
64 Charset = P.Value;
65 break;
66 }
67 }
68
69 if (Charset is null)
70 {
71 Bin = Strings.Utf8WithBom.GetBytes(s);
72 ContentType += "; charset=utf-8";
73 }
74 else
75 {
76 System.Text.Encoding Encondig = System.Text.Encoding.GetEncoding(Charset);
77 Bin = Encondig.GetBytes(s);
78 }
79 }
80 else
81 throw new ScriptRuntimeException("Expected binary or string content.", this);
82 }
83
84 return new ObjectValue(new CustomEncoding(ContentType, Bin));
85 }
86
87 }
88}
A custom encoded object.
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static KeyValuePair< string, string >[] ParseFieldValues(string Value)
Parses a set of comma or semicolon-separated field values, optionaly delimited by ' or " characters.
Definition: CommonTypes.cs:474
Static class managing binary representations of strings.
Definition: Strings.cs:10
static Encoding Utf8WithBom
UTF-8 encoding with Byte Order Mark (BOM)
Definition: Strings.cs:231
CustomEncode(ScriptNode Binary, ScriptNode ContentType, int Start, int Length, Expression Expression)
CustomEncode(Binary,Content-Type)
Definition: CustomEncode.cs:26
override string FunctionName
Name of the function
Definition: CustomEncode.cs:34
override IElement Evaluate(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function.
Definition: CustomEncode.cs:48
override string[] DefaultArgumentNames
Default Argument names
Definition: CustomEncode.cs:39
Class managing a script expression.
Definition: Expression.cs:41
Base class for funcions of one variable.
ScriptNode Argument2
Function argument 2.
ScriptNode Argument1
Function argument 1.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21