-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerExtension.cs
More file actions
128 lines (116 loc) · 4.28 KB
/
Copy pathIntegerExtension.cs
File metadata and controls
128 lines (116 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Globalization;
using Heddle.Attributes;
using Heddle.Core;
using Heddle.Data;
namespace Heddle.Extensions
{
/// <summary>
/// <para>Integer Template</para>
/// <para>Optional parameter represents default value if data name is not set in template or the object value is null</para>
/// </summary>
[ExtensionName("int")]
[DataType(typeof(int))]
[DataType(typeof(long))]
[EncodeOutput]
public class IntegerExtension : AbstractHtmlExtension
{
public override ExType InitStart(InitContext initContext, ExType dataType, ExType chainedType, ExType parent)
{
return base.InitStart(initContext, parent, chainedType, null);
}
protected override object ProcessDataInternal(in Scope scope)
{
var model = scope.ModelData;
if (model == null)
return string.Empty;
var parentScope = scope.Parent();
var format = GetInnerResult(parentScope);
if (model is int data)
{
if (!string.IsNullOrEmpty(format))
return data.ToString(format, CultureInfo.InvariantCulture);
return data.ToString(CultureInfo.InvariantCulture);
}
if (model is long longData)
{
if (!string.IsNullOrEmpty(format))
return longData.ToString(format, CultureInfo.InvariantCulture);
return longData.ToString(CultureInfo.InvariantCulture);
}
try
{
model = Convert.ChangeType(model, typeof(long), CultureInfo.InvariantCulture);
}
catch (InvalidCastException)
{
return string.Empty;
}
catch (OverflowException)
{
return string.Empty;
}
catch (FormatException)
{
return string.Empty;
}
if (!string.IsNullOrEmpty(format))
return ((long) model).ToString(format, CultureInfo.InvariantCulture);
return ((long) model).ToString(CultureInfo.InvariantCulture);
}
protected override void RenderDataInternal(in Scope scope)
{
var model = scope.ModelData;
if (model == null)
return;
var parentScope = scope.Parent();
var format = GetInnerResult(parentScope);
// Phase 8 D10: format through the one span funnel on net6+ (empty format ≡ "G" ≡ ToString(InvariantCulture));
// the coercion + its three catch-and-render-nothing handlers are unchanged, and downlevel keeps the
// string-based form verbatim (ISpanFormattable does not exist there).
if (model is int data)
{
#if NET6_0_OR_GREATER
scope.Renderer.Render(data, format, CultureInfo.InvariantCulture);
#else
scope.Renderer.Render(!string.IsNullOrEmpty(format) ? data.ToString(format, CultureInfo.InvariantCulture) : data.ToString(CultureInfo.InvariantCulture));
#endif
return;
}
if (model is long longData)
{
#if NET6_0_OR_GREATER
scope.Renderer.Render(longData, format, CultureInfo.InvariantCulture);
#else
scope.Renderer.Render(!string.IsNullOrEmpty(format)
? longData.ToString(format, CultureInfo.InvariantCulture)
: longData.ToString(CultureInfo.InvariantCulture));
#endif
return;
}
try
{
longData = (long) Convert.ChangeType(model, typeof(long), CultureInfo.InvariantCulture);
}
catch (InvalidCastException)
{
return;
}
catch (OverflowException)
{
return;
}
catch (FormatException)
{
return;
}
#if NET6_0_OR_GREATER
scope.Renderer.Render(longData, format, CultureInfo.InvariantCulture);
#else
scope.Renderer.Render(!string.IsNullOrEmpty(format)
? longData.ToString(format, CultureInfo.InvariantCulture)
: longData.ToString(CultureInfo.InvariantCulture));
#endif
}
}
}