]> O.S.I.I.S - jp/crow.git/commitdiff
allocations in Point, Size, Fill => Span
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 17 Jan 2021 19:20:18 +0000 (20:20 +0100)
committerj-p <jp_bruyere@hotmail.com>
Sat, 6 Feb 2021 19:28:02 +0000 (20:28 +0100)
Crow/src/2d/Point.cs
Crow/src/2d/Size.cs
Crow/src/Fill/Fill.cs
Crow/src/Font.cs
Crow/src/XCursor.cs
Samples/common/ui/Interfaces/Divers/testTextBox.crow
Samples/common/ui/Interfaces/TemplatedContainer/testTabView.crow
Samples/common/ui/Interfaces/TemplatedContainer/testTabView2.crow

index 02f12e2fb1b9c051a33c42d3e3383f4e00d4aa5b..c2f982bd62cbef7da2cfa02841dda1f6d118b0c3 100644 (file)
@@ -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<Point>, IEquatable<int>
     {
-               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<char> 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)));
                }
-       }
+    }
 }
index 2d1d171702f8cc62b69f66eae5113a2f60b9c861..935ff656a70f9a2a52f6d38a7fcc265ac47c86cc 100644 (file)
@@ -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<Size>, IEquatable<int>
+       {               
                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<char> 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)));
                }
-       }
+
+    }
 }
index 022f6c05a076008ba70cba35b1e280411e15beb1..24c94dcad2e0c7f6cd890526f602a8ddced2c623 100644 (file)
@@ -19,16 +19,17 @@ namespace Crow
                /// <param name="bounds">paint operation bounding box, unused for SolidColor</param>
                public abstract void SetAsSource (Interface iFace, Context ctx, Rectangle bounds = default(Rectangle));
                public static object Parse (string s){
-                       if (string.IsNullOrEmpty (s))
+                       ReadOnlySpan<char> 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));
index 89ce7edb33a8b7c6bef0804e5941440297cdcc85..c326b4eaccbe8107315693d0bc6fe17a48f9668e 100644 (file)
@@ -1,28 +1,6 @@
-//
-// Font.cs
+// Copyright (c) 2013-2021  Bruyère Jean-Philippe jp_bruyere@hotmail.com
 //
-// Author:
-//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
-//
-// 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<FontStyle> (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<char> 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<FontStyle> (tmp.Slice (ioc + 1).ToString (), true);
+                               }
+                       }
+                       return f;                       
                }
        }
 }
index 14151b8dc5464824e896de52cbc3024dbe8e5321..a987b6022252319a7a827796c96804c5c211e51e 100644 (file)
@@ -1,28 +1,6 @@
-//
-// XCursor.cs
+// Copyright (c) 2013-2021  Bruyère Jean-Philippe jp_bruyere@hotmail.com
 //
-// Author:
-//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
-//
-// 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;
index d0fe0af4e669e56b17aebbf83d1f349652956cf6..f58e800e4bcfbdb558ad50c08cfd8453cf722253 100644 (file)
@@ -1,25 +1,11 @@
 <?xml version="1.0"?>
 <VerticalStack Background="SteelBlue" Fit="true">
        <HorizontalStack Fit="true">
-               <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="TopLeft"/>
-               <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="Top"/>
-               <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="TopRight"/>
                <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="Left"/>
                <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="Center"/>
                <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="Right"/>
-               <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="BottomLeft"/>
-               <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="Bottom"/>
-               <TextBox Background="DarkGrey" Height="30" Width="50" TextAlignment="BottomRight"/>
        </HorizontalStack>
-       <HorizontalStack Fit="true">
-               <TextBox Background="DarkGrey" Height="30" Width="70" TextAlignment="Top" HorizontalStretch="true" Text="TopStretch"/>
-               <TextBox Background="DarkGrey" Height="30" Width="60" TextAlignment="Center" HorizontalStretch="true" Text="HorizontalStretch" />
-               <TextBox Background="DarkGrey" Height="30" Width="60" TextAlignment="Bottom" HorizontalStretch="true" Text="BottomStretch"/>
-               <TextBox Background="DarkGrey" Height="10" Width="70" TextAlignment="Left" VerticalStretch="true" Text="LeftStretch"/>
-               <TextBox Background="DarkGrey" Height="10" Width="70" TextAlignment="Center" VerticalStretch="true" Text="VerticalStretch"/>
-               <TextBox Background="DarkGrey" Height="10" Width="70" TextAlignment="Right" VerticalStretch="true" Text="RightStretch"/>
-               <TextBox Background="DarkGrey" Height="30" Width="50" HorizontalStretch="true" VerticalStretch="true" Text="Fit"/>
-       </HorizontalStack>      
+       
        <TextBox Name="tb" Multiline="true" Font="droid,16" Margin="5" Text="A\nBB\nCCC\nDDDD"/>
        <TextBox Multiline="true" Font="droid,16" Name="tb5" Margin="5" Text="{../tb.SelectedText}"/>
 <!--   <TextBox Font="droid,10" Name="tb1" Margin="5" Text="this is a test of a text box"/>
index e3717c82cf3eb861a061dcfb337af7bc62970025..7aa4cdd58ef9060b503e59efdacfe878d131a920 100644 (file)
@@ -24,7 +24,7 @@
                </TabItem>
                <TabItem Name="TabItem3" Background="DimGrey" Caption="tab item 3">
                        <Container Margin="5" CornerRadius="2" >
-                               <TextBox Margin="5" Multiline="true" TextAlignment="TopLeft"/>
+                               <TextBox Margin="5" Multiline="true" TextAlignment="Left"/>
                        </Container>
                </TabItem>
                <TabItem Name="TabItem4" Background="DimGrey" Caption="tab item 4" Margin="0">
index 67b87b07829c3aba91330bf94cfa9288c94ba581..8e208aea1711ecb2f91d5b8731ab9a251992aa7f 100644 (file)
@@ -24,7 +24,7 @@
                        </TabItem>
                        <TabItem Name="TabItem3" Background="DimGrey" Caption="tab-1.3">
                                <Container Margin="5" CornerRadius="2" >
-                                       <TextBox Margin="5" Multiline="true" TextAlignment="TopLeft"/>
+                                       <TextBox Margin="5" Multiline="true" TextAlignment="Left"/>
                                </Container>
                        </TabItem>
                </TabView>
@@ -55,7 +55,7 @@
                        </TabItem>
                        <TabItem Name="TabItem3" Background="DimGrey" Caption="tab-2.3">
                                <Container Margin="5" CornerRadius="2" >
-                                       <TextBox Margin="5" Multiline="true" TextAlignment="TopLeft"/>
+                                       <TextBox Margin="5" Multiline="true" TextAlignment="Left"/>
                                </Container>
                        </TabItem>
                </TabView>