Skip to content

Commit 7fdae33

Browse files
committed
Added CanBeEncodedAs method to StringUtilities
1 parent 9d8b496 commit 7fdae33

File tree

2 files changed

+73
-32
lines changed

2 files changed

+73
-32
lines changed

src/Gsemac.Text/StringUtilities.cs

+46-32
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,12 @@ public static string TrimOrDefault(string input) {
388388

389389
}
390390

391-
public static string Unescape(string input, UnescapeOptions options = UnescapeOptions.Default) {
391+
public static string Unescape(string value, UnescapeOptions options = UnescapeOptions.Default) {
392392

393-
if (string.IsNullOrEmpty(input))
394-
return input;
393+
if (string.IsNullOrEmpty(value))
394+
return value;
395395

396-
string result = input;
396+
string result = value;
397397

398398
if (options.HasFlag(UnescapeOptions.RepairTextEncoding))
399399
result = RepairTextEncoding(result);
@@ -426,59 +426,84 @@ public static string Unescape(string input, UnescapeOptions options = UnescapeOp
426426

427427
}
428428

429-
public static bool IsNumeric(string input) {
429+
public static bool IsNumeric(string value) {
430430

431-
if (string.IsNullOrWhiteSpace(input))
431+
if (string.IsNullOrWhiteSpace(value))
432432
return false;
433433

434434
// The idea behind this method is that it should return true for any string a naive observer would consider to be a number.
435435
// By default, decimals and negative numbers are considered numeric.
436436

437437
// Allow a leading a sign for negative numbers, but not positive numbers.
438438

439-
if (input.TrimStart().StartsWith("+"))
439+
if (value.TrimStart().StartsWith("+"))
440440
return false;
441441

442-
return IsNumeric(input, NumberStyles.Integer | NumberStyles.AllowDecimalPoint);
442+
return IsNumeric(value, NumberStyles.Integer | NumberStyles.AllowDecimalPoint);
443443

444444
}
445-
public static bool IsNumeric(string input, NumberStyles styles) {
445+
public static bool IsNumeric(string value, NumberStyles styles) {
446446

447-
if (string.IsNullOrWhiteSpace(input))
447+
if (string.IsNullOrWhiteSpace(value))
448448
return false;
449449

450-
return double.TryParse(input, styles, CultureInfo.InvariantCulture, out _);
450+
return double.TryParse(value, styles, CultureInfo.InvariantCulture, out _);
451451

452452
}
453-
public static string PadDigits(string num, int numberOfDigits) {
453+
public static bool IsNewLine(string value) {
454+
455+
if (string.IsNullOrEmpty(value))
456+
return false;
457+
458+
return value.Equals("\r") ||
459+
value.Equals("\n") ||
460+
value.Equals("\r\n");
461+
462+
}
463+
public static bool CanBeEncodedAs(string value, Encoding encoding) {
464+
465+
if (encoding is null)
466+
throw new ArgumentNullException(nameof(encoding));
467+
468+
if (string.IsNullOrEmpty(value))
469+
return false;
470+
471+
byte[] encodedBytes = encoding.GetBytes(value);
472+
string decodedString = encoding.GetString(encodedBytes);
473+
474+
return decodedString.Equals(value, StringComparison.InvariantCulture);
475+
476+
}
477+
478+
public static string PadDigits(string numericString, int numberOfDigits) {
454479

455480
// Trim all existing leading zeros.
456481

457-
num = (num ?? "").TrimStart('0');
482+
numericString = (numericString ?? "").TrimStart('0');
458483

459484
// Make sure the string contains at least one (whole) digit.
460485

461-
if (num.Length <= 0 || num.StartsWith("."))
462-
num = "0" + num;
486+
if (numericString.Length <= 0 || numericString.StartsWith("."))
487+
numericString = "0" + numericString;
463488

464489
// Pad the string with zeros so that the leading digits have a length of /at least/ the desired number of digits.
465490
// If there are already more leading digits than desired, no padding is added.
466491

467-
int currentLeadingDigits = num.IndexOf(".");
492+
int currentLeadingDigits = numericString.IndexOf(".");
468493

469494
if (currentLeadingDigits < 0)
470-
currentLeadingDigits = num.Length;
495+
currentLeadingDigits = numericString.Length;
471496

472497
int paddingLength = Math.Max(numberOfDigits - currentLeadingDigits, 0);
473498

474-
num = "".PadLeft(paddingLength, '0') + num;
499+
numericString = "".PadLeft(paddingLength, '0') + numericString;
475500

476-
return num;
501+
return numericString;
477502

478503
}
479-
public static string PadDigits(int num, int numberOfDigits) {
504+
public static string PadDigits(int numericString, int numberOfDigits) {
480505

481-
return PadDigits(num.ToString(CultureInfo.InvariantCulture), numberOfDigits);
506+
return PadDigits(numericString.ToString(CultureInfo.InvariantCulture), numberOfDigits);
482507

483508
}
484509

@@ -550,17 +575,6 @@ public static Stream StringToStream(string value, Encoding encoding) {
550575

551576
}
552577

553-
public static bool IsNewLine(string value) {
554-
555-
if (string.IsNullOrEmpty(value))
556-
return false;
557-
558-
return value.Equals("\r") ||
559-
value.Equals("\n") ||
560-
value.Equals("\r\n");
561-
562-
}
563-
564578
public static string GetRandomString(int length) {
565579

566580
return GetRandomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", length);

tests/Gsemac.Text.Tests/StringUtilitiesTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,33 @@ public void TestIsNumericWithNullStringAndNumberStyle() {
638638

639639
}
640640

641+
// CanBeEncodedAs
642+
643+
[TestMethod]
644+
public void TestCanBeEncodedAsWithAsciiStringAndAsciiEncoding() {
645+
646+
Assert.IsTrue(StringUtilities.CanBeEncodedAs("hello, world!", Encoding.ASCII));
647+
648+
}
649+
[TestMethod]
650+
public void TestCanBeEncodedAsWithAsciiStringAndUtf8Encoding() {
651+
652+
Assert.IsTrue(StringUtilities.CanBeEncodedAs("hello, world!", Encoding.UTF8));
653+
654+
}
655+
[TestMethod]
656+
public void TestCanBeEncodedAsWitUtf8StringAndAsciiEncoding() {
657+
658+
Assert.IsFalse(StringUtilities.CanBeEncodedAs("こんにちは世界!", Encoding.ASCII));
659+
660+
}
661+
[TestMethod]
662+
public void TestCanBeEncodedAsWitUtf8StringAndUtf8Encoding() {
663+
664+
Assert.IsTrue(StringUtilities.CanBeEncodedAs("こんにちは世界!", Encoding.UTF8));
665+
666+
}
667+
641668
// PadDigits
642669

643670
[TestMethod]

0 commit comments

Comments
 (0)