From: Jean-Philippe Bruyère Date: Sun, 17 Jan 2021 19:20:18 +0000 (+0100) Subject: allocations in Point, Size, Fill => Span X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=f0a29c1318bd3dd2a6947f8c1f13fed53d282e89;p=jp%2Fcrow.git allocations in Point, Size, Fill => Span --- diff --git a/Crow/src/2d/Point.cs b/Crow/src/2d/Point.cs index 02f12e2f..c2f982bd 100644 --- a/Crow/src/2d/Point.cs +++ b/Crow/src/2d/Point.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2013-2019 Bruyère Jean-Philippe jp_bruyere@hotmail.com +// Copyright (c) 2013-2021 Bruyère Jean-Philippe jp_bruyere@hotmail.com // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) @@ -9,16 +9,20 @@ using System.Text; namespace Crow { - public struct Point + public struct Point : IEquatable, IEquatable { - public int X; - public int Y; + public int X, Y; - public Point (int x, int y) - { + #region CTOR + public Point (int x, int y) { X = x; Y = y; } + public Point (int pos) { + X = pos; + Y = pos; + } + #endregion public int Length => (int)Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); public double LengthD => Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2)); @@ -40,10 +44,10 @@ namespace Crow public static Point operator / (Point p1, Point p2) => new Point (p1.X / p2.X, p1.Y / p2.Y); public static Point operator / (Point p, int d) => new Point (p.X / d, p.Y / d); - public static bool operator == (Point s1, Point s2) => s1.X == s2.X && s1.Y == s2.Y; - public static bool operator == (Point s, int i) => s.X == i && s.Y == i; - public static bool operator != (Point s1, Point s2) => !(s1.X == s2.X && s1.Y == s2.Y); - public static bool operator != (Point s, int i) => !(s.X == i && s.Y == i); + public static bool operator == (Point s1, Point s2) => s1.Equals (s2); + public static bool operator != (Point s1, Point s2) => !s1.Equals (s2); + public static bool operator == (Point s, int i) => s.Equals(i); + public static bool operator != (Point s, int i) => !s.Equals (i); public static bool operator > (Point p1, Point p2) => p1.X > p2.X && p1.Y > p2.Y; public static bool operator > (Point s, int i) => s.X > i && s.Y > i; public static bool operator < (Point p1, Point p2) => p1.X < p2.X && p1.Y < p2.Y; @@ -53,34 +57,21 @@ namespace Crow public static bool operator <= (Point p1, Point p2) => p1.X <= p2.X && p1.Y <= p2.Y; public static bool operator <= (Point s, int i) => s.X <= i && s.Y <= i; - public override string ToString () => string.Format ("{0},{1}", X, Y); - public override bool Equals (object obj) => obj is Point ? this == (Point)obj : - obj is Point && (Point)this == (Point)obj; - public static Point Parse (string s) - { - if (string.IsNullOrEmpty (s)) - return default (Point); - string [] d = s.Trim ().Split (','); - if (d.Length == 2) - return new Point (int.Parse (d [0]), int.Parse (d [1])); - else if (d.Length == 1) { - int tmp = int.Parse (d [0]); - return new Point (tmp, tmp); - } - throw new Exception ("Crow.PointD Parsing Error: " + s); - } + public bool Equals (Point other) => X == other.X && Y == other.Y; + public bool Equals (int other) => X == other && Y == other; - public override int GetHashCode () - { -#pragma warning disable RECS0025 // Champ autre qu’en lecture seule référencé dans « GetHashCode() » - unchecked { - var hashCode = 1861411795; - hashCode = hashCode * -1521134295 + X.GetHashCode (); - hashCode = hashCode * -1521134295 + Y.GetHashCode (); - return hashCode; - } -#pragma warning restore RECS0025 // Champ autre qu’en lecture seule référencé dans « GetHashCode() » } + public override int GetHashCode () => HashCode.Combine (X, Y); + public override bool Equals (object obj) => obj is Point s ? Equals (s) : false; + public override string ToString () => $"{X},{Y}"; + public static Point Parse (string s) { + ReadOnlySpan tmp = s.AsSpan (); + if (tmp.Length == 0) + return default (Point); + int ioc = tmp.IndexOf (','); + return ioc < 0 ? new Point (int.Parse (tmp)) : new Point ( + int.Parse (tmp.Slice (0, ioc)), + int.Parse (tmp.Slice (ioc + 1))); } - } + } } diff --git a/Crow/src/2d/Size.cs b/Crow/src/2d/Size.cs index 2d1d1717..935ff656 100644 --- a/Crow/src/2d/Size.cs +++ b/Crow/src/2d/Size.cs @@ -1,13 +1,13 @@ -// Copyright (c) 2013-2019 Bruyère Jean-Philippe jp_bruyere@hotmail.com +// Copyright (c) 2013-2021 Bruyère Jean-Philippe jp_bruyere@hotmail.com // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +using System; + namespace Crow { - public struct Size - { - public static Size Zero => new Size (0, 0); - + public struct Size : IEquatable, IEquatable + { public int Width, Height; #region CTOR @@ -27,10 +27,10 @@ namespace Crow public static implicit operator Rectangle(Size s)=> new Rectangle (s); public static implicit operator Size(int i)=> new Size(i, i); public static implicit operator string(Size s)=> s.ToString (); - public static implicit operator Size(string s)=> string.IsNullOrEmpty (s) ? Zero : Parse (s); + public static implicit operator Size(string s)=> Parse (s); - public static bool operator == (Size s1, Size s2) => (s1.Width == s2.Width && s1.Height == s2.Height); - public static bool operator != (Size s1, Size s2) => (s1.Width != s2.Width || s1.Height != s2.Height); + public static bool operator == (Size s1, Size s2) => s1.Equals(s2); + public static bool operator != (Size s1, Size s2) => !s1.Equals (s2); public static bool operator > (Size s1, Size s2) => (s1.Width > s2.Width && s1.Height > s2.Height); public static bool operator >= (Size s1, Size s2) => (s1.Width >= s2.Width && s1.Height >= s2.Height); public static bool operator < (Size s1, Size s2) => (s1.Width < s2.Width) ? s1.Height <= s2.Height : @@ -40,33 +40,31 @@ namespace Crow public static bool operator > (Size s, int i) => s.Width > i && s.Height > i; public static bool operator >= (Size s, int i) => s.Width >= i && s.Height >= i; public static bool operator <= (Size s1, Size s2) => (s1.Width <= s2.Width && s1.Height <= s2.Height); - public static bool operator == (Size s, int i) => (s.Width == i && s.Height == i); - public static bool operator != (Size s, int i) => (s.Width != i || s.Height != i); + /*public static bool operator == (Size s, int i) => (s.Width == i && s.Height == i); + public static bool operator != (Size s, int i) => (s.Width != i || s.Height != i);*/ public static Size operator + (Size s1, Size s2) => new Size (s1.Width + s2.Width, s1.Height + s2.Height); public static Size operator + (Size s, int i) => new Size (s.Width + i, s.Height + i); public static Size operator * (Size s, int i) => new Size (s.Width * i, s.Height * i); public static Size operator / (Size s, int i) => new Size (s.Width / i, s.Height / i); #endregion - public override int GetHashCode () - { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - // Suitable nullity checks etc, of course :) - hash = hash * 23 + Width.GetHashCode(); - hash = hash * 23 + Height.GetHashCode(); - return hash; - } - } - public override bool Equals (object obj) => (obj == null || obj.GetType () != typeof (Size)) ? false : this == (Size)obj; + + public bool Equals (Size other) => Width == other.Width && Height == other.Height; + public bool Equals (int other) => Width == other && Height == other; + + public override int GetHashCode () => HashCode.Combine (Width, Height); + public override bool Equals (object obj) => obj is Size s ? Equals(s) : false; public override string ToString () => $"{Width},{Height}"; public static Size Parse(string s) - { - string[] d = s.Split(','); - return d.Length == 1 ? new Size(int.Parse(d[0])) : new Size( - int.Parse(d[0]), - int.Parse(d[1])); + { + ReadOnlySpan tmp = s.AsSpan (); + if (tmp.Length == 0) + return default (Size); + int ioc = tmp.IndexOf (','); + return ioc < 0 ? new Size (int.Parse (tmp)) : new Size ( + int.Parse (tmp.Slice (0, ioc)), + int.Parse (tmp.Slice (ioc + 1))); } - } + + } } diff --git a/Crow/src/Fill/Fill.cs b/Crow/src/Fill/Fill.cs index 022f6c05..24c94dca 100644 --- a/Crow/src/Fill/Fill.cs +++ b/Crow/src/Fill/Fill.cs @@ -19,16 +19,17 @@ namespace Crow /// paint operation bounding box, unused for SolidColor public abstract void SetAsSource (Interface iFace, Context ctx, Rectangle bounds = default(Rectangle)); public static object Parse (string s){ - if (string.IsNullOrEmpty (s)) + ReadOnlySpan tmp = s.AsSpan (); + if (tmp.Length == 0) return null; - if (s.Substring (1).StartsWith ("gradient", StringComparison.Ordinal)) - return (Gradient)Gradient.Parse (s); - if (s.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture) || - s.EndsWith (".png", true, System.Globalization.CultureInfo.InvariantCulture) || - s.EndsWith (".jpg", true, System.Globalization.CultureInfo.InvariantCulture) || - s.EndsWith (".jpeg", true, System.Globalization.CultureInfo.InvariantCulture)|| - s.EndsWith (".bmp", true, System.Globalization.CultureInfo.InvariantCulture) || - s.EndsWith (".gif", true, System.Globalization.CultureInfo.InvariantCulture)) + if (tmp.Length > 8 && tmp.Slice (1, 8).SequenceEqual ("gradient")) + return Gradient.Parse (s); + if (tmp.EndsWith (".svg", StringComparison.OrdinalIgnoreCase) || + tmp.EndsWith (".png", StringComparison.OrdinalIgnoreCase) || + tmp.EndsWith (".jpg", StringComparison.OrdinalIgnoreCase) || + tmp.EndsWith (".jpeg", StringComparison.OrdinalIgnoreCase) || + tmp.EndsWith (".bmp", StringComparison.OrdinalIgnoreCase) || + tmp.EndsWith (".gif", StringComparison.OrdinalIgnoreCase)) return Picture.Parse (s); return new SolidColor((Color)Color.Parse (s)); diff --git a/Crow/src/Font.cs b/Crow/src/Font.cs index 89ce7edb..c326b4ea 100644 --- a/Crow/src/Font.cs +++ b/Crow/src/Font.cs @@ -1,28 +1,6 @@ -// -// Font.cs +// Copyright (c) 2013-2021 Bruyère Jean-Philippe jp_bruyere@hotmail.com // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using Crow.Cairo; @@ -84,42 +62,36 @@ namespace Crow } #region Operators - public static implicit operator string(Font c) - { - return c.ToString(); - } - public static implicit operator Font(string s) - { - Font f = new Font (); - - if (!string.IsNullOrEmpty (s)) { - string[] c = s.TrimStart().TrimEnd().Split (','); - - if (c.Length == 2) - f.Size = int.Parse (c [1].TrimStart()); - - string[] n = c [0].TrimEnd().Split (' '); - - f.Name = n [0]; - - if (n.Length > 1) - f.Style = FastEnum.Parse (n[n.Length-1], true); - } - - return f; - } + public static implicit operator string(Font c) => c.ToString(); + + public static implicit operator Font(string s) => (Font)Parse(s); #endregion - public override string ToString() + public override string ToString () => + _style == FontStyle.Normal ? $"{_name},{_size}" : $"{_name} {_style},{_size}"; + + public static object Parse(string s) { + Font f = new Font (); + ReadOnlySpan tmp = s.AsSpan ().Trim (); + if (tmp.Length > 0) { + int ioc = tmp.IndexOf (','); - return (_style == FontStyle.Normal) ? $"{_name},{_size}" : $"{_name} {_style},{_size}"; + if (ioc >= 0) { + f.Size = int.Parse (tmp.Slice (ioc + 1).Trim ()); + tmp = tmp.Slice (0, ioc).TrimEnd (); + } - } + ioc = tmp.IndexOf (' '); - public static object Parse(string s) - { - return (Font)s; + if (ioc < 0) + f.Name = tmp.ToString (); + else { + f.Name = tmp.Slice (0, ioc).ToString (); + f.Style = FastEnum.Parse (tmp.Slice (ioc + 1).ToString (), true); + } + } + return f; } } } diff --git a/Crow/src/XCursor.cs b/Crow/src/XCursor.cs index 14151b8d..a987b602 100644 --- a/Crow/src/XCursor.cs +++ b/Crow/src/XCursor.cs @@ -1,28 +1,6 @@ -// -// XCursor.cs +// Copyright (c) 2013-2021 Bruyère Jean-Philippe jp_bruyere@hotmail.com // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.IO; diff --git a/Samples/common/ui/Interfaces/Divers/testTextBox.crow b/Samples/common/ui/Interfaces/Divers/testTextBox.crow index d0fe0af4..f58e800e 100644 --- a/Samples/common/ui/Interfaces/Divers/testTextBox.crow +++ b/Samples/common/ui/Interfaces/Divers/testTextBox.crow @@ -1,25 +1,11 @@  - - - - - - - - - - - - - - - +