]> O.S.I.I.S - jp/crow.git/commitdiff
use StringBuilder in StyleReader
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 19 Jan 2021 12:10:25 +0000 (13:10 +0100)
committerj-p <jp_bruyere@hotmail.com>
Sat, 6 Feb 2021 19:28:02 +0000 (20:28 +0100)
Crow/src/Colors.cs
Crow/src/styling/StyleReader.cs

index ebdfd198427e8601601806d7c5b402f2163932ec..eb62cb1406ffd7156ce4aabe72a0c32b2b07427b 100644 (file)
@@ -351,7 +351,7 @@ namespace Crow
                }
 
                public static object Parse(string s)
-                       => (string.IsNullOrEmpty (s)) ? new Color (Colors.White) :
+                       => string.IsNullOrEmpty (s) ? new Color (Colors.White) :
                                s[0] == '#' ? new Color (UInt32.Parse (s.AsSpan().Slice (1), System.Globalization.NumberStyles.HexNumber)) :
                                char.IsDigit(s[0]) ? FromIml (s) :
                                FastEnum.TryParse<Colors> (s, out Colors cc) ? new Color(cc) :
index 2602b7a8a3d911085935de434131d67e72500b6d..21cddace6db08d8f510507d970d5869185de60c4 100644 (file)
@@ -9,6 +9,7 @@ using System.Reflection;
 using System.Text.RegularExpressions;
 using System.Globalization;
 using Crow.Coding;
+using System.Text;
 
 namespace Crow
 {
@@ -74,9 +75,11 @@ namespace Crow
                        curState = States.classNames;
 
                        //string styleKey = resId.Substring (0, resId.Length - 6);
-                       string token = "";
+                       StringBuilder token = new StringBuilder(128);
+                       StringBuilder constantId = new StringBuilder (128);
+
                        List<string> targetsClasses = new List<string> ();
-                       string currentProperty = "";
+                       string currentProperty = null;
 
                        while (!EndOfStream) {
                                SkipWhiteSpaceAndLineBreak ();
@@ -92,18 +95,18 @@ namespace Crow
                                        break;
                                case ',':
                                        ReadChar ();
-                                       if (!(curState == States.classNames) || string.IsNullOrEmpty (token))
+                                       if (!(curState == States.classNames) || token.Length == 0)
                                                throw new ParserException (line, column, "Unexpected char ','", resId);
-                                       targetsClasses.Add (token);
-                                       token = "";
+                                       targetsClasses.Add (token.ToString());
+                                       token.Clear();
                                        curState = States.classNames;
                                        break;
                                case '{':
                                        ReadChar ();
-                                       if (curState != States.classNames || string.IsNullOrEmpty (token))
+                                       if (curState != States.classNames || token.Length == 0)
                                                throw new ParserException (line, column, "Unexpected char '{'", resId);
-                                       targetsClasses.Add (token);
-                                       token = "";
+                                       targetsClasses.Add (token.ToString());
+                                       token.Clear();
                                        curState = States.members;
                                        break;
                                case '}':
@@ -115,10 +118,10 @@ namespace Crow
                                        break;
                                case '=':
                                        ReadChar ();
-                                       if (!(curState == States.members || curState == States.classNames) || string.IsNullOrEmpty (token))
+                                       if (!(curState == States.members || curState == States.classNames) || token.Length == 0)
                                                throw new ParserException (line, column, "Unexpected char '='", resId);
-                                       currentProperty = token;
-                                       token = "";
+                                       currentProperty = token.ToString ();
+                                       token.Clear ();
                                        curState = States.value;
                                        break;
                                case '"':
@@ -131,24 +134,27 @@ namespace Crow
                                                if (c == '$') {
                                                        if (PeekChar () == '{') {
                                                                ReadChar ();
-                                                               //constant replacement
-                                                               string constantId = "";
+                                                               //constant replacement                                                          
                                                                while (!EndOfStream) {
                                                                        c = ReadChar ();
                                                                        if (c == '}')
                                                                                break;
-                                                                       constantId += c;
+                                                                       constantId.Append (c);
                                                                }
-                                                               if (string.IsNullOrEmpty (constantId) || !StylingConstants.ContainsKey (constantId))
+                                                               if (constantId.Length == 0)
                                                                        throw new ParserException (line, column, "Empty constant id in styling", resId);
-                                                               token += StylingConstants [constantId];
+                                                               string cst = constantId.ToString ();
+                                                               constantId.Clear ();
+                                                               if (!StylingConstants.ContainsKey (cst))
+                                                                       throw new ParserException (line, column, $"Constant id not found in styling ({cst})", resId);
+                                                               token.Append (StylingConstants[cst]);
                                                                continue;
                                                        }
                                                } else if (c == '\"') {
                                                        curState = States.endOfStatement;
                                                        break;
                                                }
-                                               token += c;
+                                               token.Append (c);
                                        }
                                        break;
                                case ';':
@@ -157,21 +163,21 @@ namespace Crow
                                        ReadChar ();
                                        if (targetsClasses.Count == 0) {
                                                //style constants
-                                               StylingConstants[currentProperty] = token;
+                                               StylingConstants[currentProperty] = token.ToString ();
                                                curState = States.classNames;
                                        } else {
                                                foreach (string tc in targetsClasses) {
                                                        if (!Styling.ContainsKey (tc))
                                                                Styling [tc] = new Style ();
-                                                       Styling [tc] [currentProperty] = token;
+                                                       Styling[tc][currentProperty] = token.ToString ();
 #if DESIGN_MODE
                                                        Styling [tc].Locations[currentProperty] = new FileLocation(resId, line, column - token.Length - 1, token.Length);
 #endif
                                                }
                                                curState = States.members;
                                        }
-                                       token = "";
-                                       currentProperty = "";
+                                       token.Clear ();
+                                       currentProperty = null;
                                        break;
                                default:
                                        if (curState == States.value)
@@ -180,9 +186,9 @@ namespace Crow
                                                throw new ParserException (line, column, "expecting end of statement", resId);
 
                                        if (nextCharIsValidCharStartName) {
-                                               token += ReadChar ();
+                                               token.Append (ReadChar ());
                                                while (nextCharIsValidCharName)
-                                                       token += ReadChar ();
+                                                       token.Append (ReadChar ());
                                        }
                                        break;
                                }