NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
ParserUtils.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Globalization;
10using System.Linq;
11
12namespace NanoXLSX.Utils
13{
18 public static class ParserUtils
19 {
20 #region constants
21
25 public const string NumericFormat = "G";
26
33 public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;
34
35 #endregion
36
43 public static bool StartsWith(string input, string value)
44 {
45 if (input == null && value == null)
46 {
47 return true;
48 }
49 else if (input == null && value != null)
50 {
51 return false;
52 }
53 else if (value == null)
54 {
55 return false;
56 }
57 return input.StartsWith(value, StringComparison.InvariantCulture);
58 }
59
66 public static bool NotStartsWith(string input, string value)
67 {
68 return !StartsWith(input, value);
69 }
70
76 public static string ToUpper(string input)
77 {
78 return !string.IsNullOrEmpty(input) ? input.ToUpper(InvariantCulture) : input;
79 }
80
86 public static string ToLower(string input)
87 {
88 return !string.IsNullOrEmpty(input) ? input.ToLower(InvariantCulture) : input;
89 }
90
96 public static string ToString(int input)
97 {
98 return input.ToString(NumericFormat, InvariantCulture);
99 }
100
106 public static string ToString(float input)
107 {
108 return input.ToString(NumericFormat, InvariantCulture);
109 }
110
116 public static string ToString(byte input)
117 {
118 return input.ToString(NumericFormat, InvariantCulture);
119 }
120
126 public static string ToString(sbyte input)
127 {
128 return input.ToString(NumericFormat, InvariantCulture);
129 }
130
136 public static string ToString(double input)
137 {
138 return input.ToString(NumericFormat, InvariantCulture);
139 }
140
146 public static string ToString(decimal input)
147 {
148 return input.ToString(NumericFormat, InvariantCulture);
149 }
150
156 public static string ToString(uint input)
157 {
158 return input.ToString(NumericFormat, InvariantCulture);
159 }
160
166 public static string ToString(long input)
167 {
168 return input.ToString(NumericFormat, InvariantCulture);
169 }
170
176 public static string ToString(ulong input)
177 {
178 return input.ToString(NumericFormat, InvariantCulture);
179 }
180
186 public static string ToString(short input)
187 {
188 return input.ToString(NumericFormat, InvariantCulture);
189 }
190
196 public static string ToString(ushort input)
197 {
198 return input.ToString(NumericFormat, InvariantCulture);
199 }
200
206 public static string NormalizeNewLines(string value)
207 {
208 if (value == null || (!value.Contains('\n') && !value.Contains('\r')))
209 {
210 return value;
211 }
212 return value.Replace("\n\r", "\n").Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
213 }
214
220 public static float ParseFloat(string rawValue)
221 {
222 return float.Parse(rawValue, InvariantCulture);
223 }
224
230 public static int ParseInt(string rawValue)
231 {
232 return int.Parse(rawValue, NumberStyles.Any, InvariantCulture);
233 }
234
240 public static int ParseBinaryBool(String rawValue)
241 {
242 if (string.IsNullOrEmpty(rawValue))
243 {
244 return 0;
245 }
246 int value;
247 if (TryParseInt(rawValue, out value))
248 {
249 if (value >= 1)
250 {
251 return 1;
252 }
253 else
254 {
255 return 0;
256 }
257 }
258 rawValue = rawValue.ToLower(InvariantCulture);
259 if (rawValue == "true")
260 {
261 return 1;
262 }
263 else
264 {
265 return 0;
266 }
267 }
268
275 public static bool TryParseInt(string rawValue, out int parsedValue)
276 {
277 return int.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
278 }
279
286 public static bool TryParseUint(string rawValue, out uint parsedValue)
287 {
288 return uint.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
289 }
290
297 public static bool TryParseLong(string rawValue, out long parsedValue)
298 {
299 return long.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
300 }
301
308 public static bool TryParseUlong(string rawValue, out ulong parsedValue)
309 {
310 return ulong.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
311 }
312
319 public static bool TryParseFloat(string rawValue, out float parsedValue)
320 {
321 return float.TryParse(rawValue, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedValue);
322 }
323
330 public static bool TryParseDecimal(string rawValue, out decimal parsedValue)
331 {
332 return decimal.TryParse(rawValue, NumberStyles.Float, InvariantCulture, out parsedValue);
333 }
334
341 public static bool TryParseDouble(string rawValue, out double parsedValue)
342 {
343 return double.TryParse(rawValue, NumberStyles.Any, InvariantCulture, out parsedValue);
344 }
345 }
346}
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToString(long input)
Transforms a long to an invariant sting.
const string NumericFormat
Numeric format for ToString conversions. This format ensures that a numeric value is printed in a lan...
static string ToString(byte input)
Transforms a byte to an invariant sting.
static readonly CultureInfo InvariantCulture
Constant for number conversion. The invariant culture (represents mostly the US numbering scheme) ens...
static bool TryParseDecimal(string rawValue, out decimal parsedValue)
Tries to parse a decimal (with float parsing style) independent from the culture info of the host.
static string ToString(double input)
Transforms a double to an invariant sting.
static bool TryParseFloat(string rawValue, out float parsedValue)
Tries to parse a float (with any parsing style) independent from the culture info of the host.
static string ToString(ulong input)
Transforms a ulong to an invariant sting.
static bool StartsWith(string input, string value)
Determines whether a string starts with a specific value.
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
static string ToString(short input)
Transforms a short to an invariant sting.
static string ToString(sbyte input)
Transforms a sbyte to an invariant sting.
static string ToString(decimal input)
Transforms a decimal to an invariant sting.
static string ToLower(string input)
Transforms a string to lower case with null check and invariant culture.
static bool TryParseInt(string rawValue, out int parsedValue)
Tries to parse an int independent of the culture info of the host.
static bool TryParseLong(string rawValue, out long parsedValue)
Tries to parse a long independent from the culture info of the host.
static bool TryParseUlong(string rawValue, out ulong parsedValue)
Tries to parse an unsigned long (ulong) independent from the culture info of the host.
static string ToString(uint input)
Transforms a uint to an invariant sting.
static string ToString(ushort input)
Transforms a ushort to an invariant sting.
static float ParseFloat(string rawValue)
Parses a float independent from the culture info of the host.
static bool NotStartsWith(string input, string value)
Determines whether a string does not start with a specific value.
static int ParseInt(string rawValue)
Parses an int independent from the culture info of the host.
static bool TryParseUint(string rawValue, out uint parsedValue)
Tries to parse an unsigned int (uint) independent from the culture info of the host.
static string ToString(float input)
Transforms a float to an invariant sting.
static string NormalizeNewLines(string value)
Normalizes all newlines of a string to CR+LF.
static int ParseBinaryBool(String rawValue)
Parses a bool as a binary number either based on an int (0/1) or a string expression (true/ false),...
static string ToString(int input)
Transforms an integer to an invariant sting.
static bool TryParseDouble(string rawValue, out double parsedValue)
Tries to parse a double (with any parsing style) independent from the culture info of the host.