|
| 1 | +/************************************************************************************************* |
| 2 | + Required Notice: Copyright (C) EPPlus Software AB. |
| 3 | + This software is licensed under PolyForm Noncommercial License 1.0.0 |
| 4 | + and may only be used for noncommercial purposes |
| 5 | + https://polyformproject.org/licenses/noncommercial/1.0.0/ |
| 6 | +
|
| 7 | + A commercial license to use this software can be purchased at https://epplussoftware.com |
| 8 | + ************************************************************************************************* |
| 9 | + Date Author Change |
| 10 | + ************************************************************************************************* |
| 11 | + 06/27/2025 EPPlus Software AB Improved format handling |
| 12 | + *************************************************************************************************/ |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Linq; |
| 16 | +using System.Text; |
| 17 | + |
| 18 | +namespace OfficeOpenXml.Style.XmlAccess |
| 19 | +{ |
| 20 | + internal static class BracketTextValidator |
| 21 | + { |
| 22 | + private static HashSet<string> _colors = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase) { "Black", "Green", "White", "Blue", "Magenta", "Yellow", "Cyan", "Red" }; |
| 23 | + |
| 24 | + private static HashSet<char> _operators = new HashSet<char> { '>', '<', '=' }; |
| 25 | + |
| 26 | + public static bool IsValid(string text) |
| 27 | + { |
| 28 | + if(string.IsNullOrEmpty(text)) return false; |
| 29 | + if (_colors.Contains(text)) return true; |
| 30 | + if (text.StartsWith("$")) return true; |
| 31 | + if(IsCondition(text)) return true; |
| 32 | + // indexed colors |
| 33 | + if (text.ToLowerInvariant().StartsWith("color") && int.TryParse(text.Substring(5), out int n) && n >= 1 && n <= 56) return true; |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + public static bool IsCondition(string text) |
| 38 | + { |
| 39 | + |
| 40 | + if (string.IsNullOrEmpty(text)) return false; |
| 41 | + |
| 42 | + text = text.Replace(" ", ""); |
| 43 | + |
| 44 | + int i = 0; |
| 45 | + if (text.StartsWith(">=")) i = 2; |
| 46 | + else if (text.StartsWith("<=")) i = 2; |
| 47 | + else if (text.StartsWith(">") || text.StartsWith("<") || text.StartsWith("=")) i = 1; |
| 48 | + else return false; |
| 49 | + |
| 50 | + if (i >= text.Length) return false; |
| 51 | + |
| 52 | + bool isValidNumber = false; |
| 53 | + bool hasDecimal = false; |
| 54 | + |
| 55 | + if (text[i] == '-') i++; // tillåt negativt tal |
| 56 | + |
| 57 | + for (; i < text.Length; i++) |
| 58 | + { |
| 59 | + char c = text[i]; |
| 60 | + if (char.IsDigit(c)) |
| 61 | + { |
| 62 | + isValidNumber = true; |
| 63 | + } |
| 64 | + else if (c == '.' && !hasDecimal) |
| 65 | + { |
| 66 | + hasDecimal = true; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return isValidNumber; |
| 75 | + |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments