Exemple #1
0
        public void Compare()
        {
            using (new CultureSwapper(PredefinedCulture.EnglishUnitedStates))
            {
                Assert.AreEqual(0, Math.Sign(Substring.Compare("abcdef", 2, 2, "CD", 0, 2, StringComparison.CurrentCultureIgnoreCase)));
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("abcdef", 1, 3, "CD", 0, 2, StringComparison.OrdinalIgnoreCase)),
                                "'b' should be less than 'C' for case-insensitive comparison");
                Assert.AreEqual(1, Math.Sign(Substring.Compare("abcdef", 1, 3, "CD", 0, 2, StringComparison.Ordinal)),
                                "'b' should be greater than 'C' for case-sensitive comparison");
                Assert.AreEqual(0, Math.Sign(Substring.Compare("Encyclopædia", 8, 2, "aedia", 0, 3, StringComparison.CurrentCulture)));

                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 5, StringComparison.CurrentCulture)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 5, StringComparison.CurrentCultureIgnoreCase)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 5, StringComparison.InvariantCulture)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 5, StringComparison.InvariantCultureIgnoreCase)));
                Assert.AreEqual(1, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 5, StringComparison.Ordinal)));
                Assert.AreEqual(1, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 5, StringComparison.OrdinalIgnoreCase)));

                Assert.AreEqual(-1, Math.Sign(Substring.Compare("café", 0, 4, "CAFE\u0301", 0, 5, StringComparison.CurrentCulture)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 0, 4, "CAFE\u0301", 0, 5, StringComparison.CurrentCultureIgnoreCase)));
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("café", 0, 4, "CAFE\u0301", 0, 5, StringComparison.InvariantCulture)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 0, 4, "CAFE\u0301", 0, 5, StringComparison.InvariantCultureIgnoreCase)));
                Assert.AreEqual(1, Math.Sign(Substring.Compare("café", 0, 4, "CAFE\u0301", 0, 5, StringComparison.Ordinal)));
                Assert.AreEqual(1, Math.Sign(Substring.Compare("café", 0, 4, "CAFE\u0301", 0, 5, StringComparison.OrdinalIgnoreCase)));

                Assert.AreEqual(0, Math.Sign(Substring.Compare("café", 2, 2, "cafe\u0301", 2, 3, StringComparison.CurrentCulture)));
                Assert.AreEqual(1, Math.Sign(Substring.Compare("café", 0, 4, "cafe\u0301", 0, 4, StringComparison.CurrentCulture)));

                Assert.AreEqual(1, Math.Sign(Substring.Compare("abc", 1, 2, "", 0, 0, StringComparison.Ordinal)));
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("", 0, 0, "abc", 0, 2, StringComparison.Ordinal)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("abc", 1, 0, "", 0, 0, StringComparison.Ordinal)));
                Assert.AreEqual(0, Math.Sign(Substring.Compare("abc123", 2, 3, "ABC123", 2, 3, StringComparison.OrdinalIgnoreCase)));
            }
        }
Exemple #2
0
            /// <summary>
            /// Whether the given control matches the constraints of this path component.
            /// </summary>
            /// <param name="control">Control to match against the path spec.</param>
            /// <returns></returns>
            public bool Matches(InputControl control)
            {
                // Match layout.
                if (!layout.isEmpty)
                {
                    // Check for direct match.
                    var layoutMatches = Substring.Compare(layout, control.layout,
                                                          StringComparison.InvariantCultureIgnoreCase) == 0;
                    if (!layoutMatches)
                    {
                        // No direct match but base layout may match.
                        var baseLayout = control.m_Layout;
                        while (InputControlLayout.s_Layouts.baseLayoutTable.TryGetValue(baseLayout, out baseLayout) && !layoutMatches)
                        {
                            layoutMatches = Substring.Compare(layout, baseLayout.ToString(),
                                                              StringComparison.InvariantCultureIgnoreCase) == 0;
                        }
                    }

                    if (!layoutMatches)
                    {
                        return(false);
                    }
                }

                // Match usage.
                if (!usage.isEmpty)
                {
                    var usages         = control.usages;
                    var haveUsageMatch = false;
                    for (var i = 0; i < usages.Count; ++i)
                    {
                        if (Substring.Compare(usages[i].ToString(), usage, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            haveUsageMatch = true;
                            break;
                        }
                    }

                    if (!haveUsageMatch)
                    {
                        return(false);
                    }
                }

                // Match name.
                if (!name.isEmpty && !isWildcard)
                {
                    if (Substring.Compare(control.name, name, StringComparison.InvariantCultureIgnoreCase) != 0)
                    {
                        return(false);
                    }
                }

                return(true);
            }
Exemple #3
0
        public void Compare_ÆbleApple()
        {
            // Sorting and String Comparison in .NET Framework
            // https://msdn.microsoft.com/en-us/goglobal/bb688122.aspx#euf

            using (new CultureSwapper(PredefinedCulture.EnglishUnitedStates))
            {
                string message = "\"Æble\" is less than \"Apple\" for the English culture.";
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("Æble", 0, 4, "Apple", 0, 5, StringComparison.CurrentCulture)), message);
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("Æble", 0, 4, "Apple", 0, 5, StringComparison.CurrentCultureIgnoreCase)), message);
            }

            using (new CultureSwapper(PredefinedCulture.DanishDenmark))
            {
                string message = "\"Æble\" is less than \"Apple\" for the invariant culture.";
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("Æble", 0, 4, "Apple", 0, 5, StringComparison.InvariantCulture)), message);
                Assert.AreEqual(-1, Math.Sign(Substring.Compare("Æble", 0, 4, "Apple", 0, 5, StringComparison.InvariantCulture)), message);

                message = "\"Æble\" is greater than \"Apple\" for the Danish culture.";
                Assert.AreEqual(1, Math.Sign(Substring.Compare("Æble", 0, 4, "Apple", 0, 5, StringComparison.CurrentCulture)), message);
                Assert.AreEqual(1, Math.Sign(Substring.Compare("Æble", 0, 4, "Apple", 0, 5, StringComparison.CurrentCultureIgnoreCase)), message);
            };
        }
Exemple #4
0
        /// <summary>
        /// Find an <see cref="InputAction">action</see> by its name in of of the <see cref="InputActionMap">
        /// action maps</see> in the asset.
        /// </summary>
        /// <param name="name">Name of the action as either a "map/action" combination (e.g. "gameplay/fire") or
        /// a simple name. In the former case, the name is split at the '/' slash and the first part is used to find
        /// a map with that name and the second part is used to find an action with that name inside the map. In the
        /// latter case, all maps are searched in order and the first action that has the given name in any of the maps
        /// is returned. Note that name comparisons are case-insensitive.</param>
        /// <returns>The action with the corresponding name or null if no matching action could be found.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is null or empty.</exception>
        /// <remarks>
        /// Does not allocate.
        /// </remarks>
        /// <example>
        /// <code>
        /// var asset = ScriptableObject.CreateInstance&lt;InputActionAsset&gt;();
        ///
        /// var map1 = new InputActionMap("map1");
        /// var map2 = new InputActionMap("map2");
        ///
        /// asset.AddActionMap(map1);
        /// asset.AddActionMap(map2);
        ///
        /// var action1 = map1.AddAction("action1");
        /// var action2 = map1.AddAction("action2");
        /// var action3 = map2.AddAction("action3");
        ///
        /// // Search all maps in the asset for any action that has the given name.
        /// asset.FindAction("action1") // Returns action1.
        /// asset.FindAction("action2") // Returns action2
        /// asset.FindAction("action3") // Returns action3.
        ///
        /// // Search for a specific action in a specific map.
        /// asset.FindAction("map1/action1") // Returns action1.
        /// asset.FindAction("map2/action2") // Returns action2.
        /// asset.FindAction("map3/action3") // Returns action3.
        /// </code>
        /// </example>
        public InputAction FindAction(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (m_ActionMaps == null)
            {
                return(null);
            }

            // Check if we have a "map/action" path.
            var indexOfSlash = name.IndexOf('/');

            if (indexOfSlash == -1)
            {
                // No slash so it's just a simple action name.
                for (var i = 0; i < m_ActionMaps.Length; ++i)
                {
                    var actions = m_ActionMaps[i].m_Actions;
                    for (var n = 0; n < actions.Length; ++n)
                    {
                        var action = actions[n];
                        if (string.Compare(action.name, name, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            return(action);
                        }
                    }
                }
            }
            else
            {
                // Have a path. First search for the map, then for the action.
                var mapName    = new Substring(name, 0, indexOfSlash);
                var actionName = new Substring(name, indexOfSlash + 1);

                if (mapName.isEmpty || actionName.isEmpty)
                {
                    throw new ArgumentException("Malformed action path: " + name, "name");
                }

                for (var i = 0; i < m_ActionMaps.Length; ++i)
                {
                    var map = m_ActionMaps[i];
                    if (Substring.Compare(map.name, mapName, StringComparison.InvariantCultureIgnoreCase) != 0)
                    {
                        continue;
                    }

                    var actions = map.m_Actions;
                    for (var n = 0; n < actions.Length; ++n)
                    {
                        var action = actions[n];
                        if (Substring.Compare(action.name, actionName, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            return(action);
                        }
                    }

                    break;
                }
            }

            return(null);
        }
Exemple #5
0
            /// <summary>
            /// Whether the given control matches the constraints of this path component.
            /// </summary>
            /// <param name="control">Control to match against the path spec.</param>
            /// <returns></returns>
            public bool Matches(InputControl control)
            {
                // Match layout.
                if (!layout.isEmpty)
                {
                    // Check for direct match.
                    var layoutMatches = Substring.Compare(layout, control.layout,
                                                          StringComparison.InvariantCultureIgnoreCase) == 0;
                    if (!layoutMatches)
                    {
                        // No direct match but base layout may match.
                        var baseLayout = control.m_Layout;
                        while (InputControlLayout.s_Layouts.baseLayoutTable.TryGetValue(baseLayout, out baseLayout) && !layoutMatches)
                        {
                            layoutMatches = Substring.Compare(layout, baseLayout.ToString(),
                                                              StringComparison.InvariantCultureIgnoreCase) == 0;
                        }
                    }

                    if (!layoutMatches)
                    {
                        return(false);
                    }
                }

                // Match usage.
                if (usages != null)
                {
                    for (int i = 0; i < usages.Length; ++i)
                    {
                        if (!usages[i].isEmpty)
                        {
                            var controlUsages  = control.usages;
                            var haveUsageMatch = false;
                            for (var ci = 0; ci < controlUsages.Count; ++ci)
                            {
                                if (Substring.Compare(controlUsages[ci].ToString(), usages[i], StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    haveUsageMatch = true;
                                    break;
                                }
                            }

                            if (!haveUsageMatch)
                            {
                                return(false);
                            }
                        }
                    }
                }

                // Match name.
                if (!name.isEmpty && !isWildcard)
                {
                    ////FIXME: unlike the matching path we have in MatchControlsRecursive, this does not take aliases into account
                    if (Substring.Compare(control.name, name, StringComparison.InvariantCultureIgnoreCase) != 0)
                    {
                        return(false);
                    }
                }

                // Match display name.
                if (!displayName.isEmpty)
                {
                    if (Substring.Compare(control.displayName, displayName,
                                          StringComparison.InvariantCultureIgnoreCase) != 0)
                    {
                        return(false);
                    }
                }

                return(true);
            }