NanoXLSX.Core 3.0.0-rc.4
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
221 public static float ParseFloat(string rawValue)
222 {
223 return float.Parse(rawValue, InvariantCulture);
224 }
225
232 public static int ParseInt(string rawValue)
233 {
234 return int.Parse(rawValue, NumberStyles.Any, InvariantCulture);
235 }
236
243 public static double ParseDouble(string rawValue)
244 {
245 return double.Parse(rawValue, InvariantCulture);
246 }
247
253 public static int ParseBinaryBool(String rawValue)
254 {
255 if (string.IsNullOrEmpty(rawValue))
256 {
257 return 0;
258 }
259 int value;
260 if (TryParseInt(rawValue, out value))
261 {
262 if (value >= 1)
263 {
264 return 1;
265 }
266 else
267 {
268 return 0;
269 }
270 }
271 rawValue = rawValue.ToLower(InvariantCulture);
272 if (rawValue == "true")
273 {
274 return 1;
275 }
276 else
277 {
278 return 0;
279 }
280 }
281
288 public static bool TryParseInt(string rawValue, out int parsedValue)
289 {
290 return int.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
291 }
292
299 public static bool TryParseUint(string rawValue, out uint parsedValue)
300 {
301 return uint.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
302 }
303
310 public static bool TryParseLong(string rawValue, out long parsedValue)
311 {
312 return long.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
313 }
314
321 public static bool TryParseUlong(string rawValue, out ulong parsedValue)
322 {
323 return ulong.TryParse(rawValue, NumberStyles.Integer, InvariantCulture, out parsedValue);
324 }
325
332 public static bool TryParseFloat(string rawValue, out float parsedValue)
333 {
334 return float.TryParse(rawValue, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedValue);
335 }
336
343 public static bool TryParseDecimal(string rawValue, out decimal parsedValue)
344 {
345 return decimal.TryParse(rawValue, NumberStyles.Float, InvariantCulture, out parsedValue);
346 }
347
354 public static bool TryParseDouble(string rawValue, out double parsedValue)
355 {
356 return double.TryParse(rawValue, NumberStyles.Any, InvariantCulture, out parsedValue);
357 }
358 }
359}
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 double ParseDouble(string rawValue)
Parses a double independent from the culture info of the host.
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.