Repository navigation
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
109 lines (93 loc) · 3.6 KB
/
Copy pathStringExtensions.cs
File metadata and controls
109 lines (93 loc) · 3.6 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
using System;
using System.Globalization;
using System.Linq;
namespace DotNetCSS
{
internal static class StringExtensions
{
public static bool Has(this string value, char chr, int index = 0)
{
return (value != null) && (value.Length > index) && (value[index] == chr);
}
public static bool Contains(this string[] list, string element,
StringComparison comparison = StringComparison.Ordinal)
{
return list.Any(t => t.Equals(element, comparison));
}
public static bool Is(this string current, string other)
{
return string.Equals(current, other, StringComparison.Ordinal);
}
public static bool Isi(this string current, string other)
{
return string.Equals(current, other, StringComparison.OrdinalIgnoreCase);
}
public static bool IsOneOf(this string element, string item1, string item2)
{
return element.Is(item1) || element.Is(item2);
}
public static string StylesheetString(this string value)
{
var builder = Pool.NewStringBuilder();
builder.Append(Symbols.DoubleQuote);
if (!string.IsNullOrEmpty(value))
{
for (var i = 0; i < value.Length; i++)
{
var character = value[i];
switch (character)
{
case Symbols.Null:
throw new ParseException("Unable to parse null sumbol");
case Symbols.DoubleQuote:
case Symbols.ReverseSolidus:
builder.Append(Symbols.ReverseSolidus).Append(character);
break;
default:
if (character.IsInRange(0x1, 0x1f) || (character == (char) 0x7b))
{
builder.Append(Symbols.ReverseSolidus)
.Append(character.ToHex())
.Append(i + 1 != value.Length ? " " : "");
}
else
{
builder.Append(character);
}
break;
}
}
}
builder.Append(Symbols.DoubleQuote);
return builder.ToPool();
}
public static string StylesheetFunction(this string value, string argument)
{
return string.Concat(value, "(", argument, ")");
}
public static string StylesheetUrl(this string value)
{
var argument = value.StylesheetString();
return FunctionNames.Url.StylesheetFunction(argument);
}
public static string StylesheetUnit(this string value, out float result)
{
if (!string.IsNullOrEmpty(value))
{
var firstLetter = value.Length;
while (!value[firstLetter - 1].IsDigit() && (--firstLetter > 0))
{
// Intentional empty.
}
var parsed = float.TryParse(value.Substring(0, firstLetter), NumberStyles.Any,
CultureInfo.InvariantCulture, out result);
if ((firstLetter > 0) && parsed)
{
return value.Substring(firstLetter);
}
}
result = default(float);
return null;
}
}
}