Windowscommunitytoolkit: Extension (IsCharacterString()) give false state when I enter space or tap

Created on 5 May 2020  路  10Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

In Extensions session (IsCharacterString()) when I enter space or tap in textbox that check if all characters inside it are only letters not numbers or operators -> give me false state such as:
textbox.Text = Howareyou? -> true
textbox.Text = How are you? -> false

extensions feature request help wanted question

All 10 comments

Hello AbdAlghaniAlbiek, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 馃檶

@AbdAlghaniAlbiek can you please provide more detail on this? The steps you are taking or add a screenshot would be great.

I hope that but github doesn't support uploading images
I'll give you an example on my situation:

sting testText = Hello Kyaa;
if(testText.IsCharacterString()){
Debug.Writline("Valid") ;
}
else{
Debug.Writeline("Not valid");
}

==> Not valid

you can see that there isn't numbers or operators it should be valid but when I enter on space or tap it give me Not valid
I want to ignore spaces and tabs in text

@AbdAlghaniAlbiek is that the code you are using? Seems like 'Writeline' is missing an e in the third line and string in first?

No this is just example I have written on github

@AbdAlghaniAlbiek That makes sense. Thanks for clarifying 馃憤

@AbdAlghaniAlbiek the purpose of this method was to validate letter characters only (a 'space' is not a letter); it doesn't accept spaces as valid input as you've discovered by design.

The RegEx driving this is here:

https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/c65d6486952dec37f4a219ec177eb2709bb51c79/Microsoft.Toolkit/Extensions/StringExtensions.cs#L25

If you'd like, you could open a PR to add an additional method to accept whitespace like:

public static bool ContainsCharactersOrWhiteSpace(this string str) ...

We should probably deprecate IsCharacterString in 6.1 and add an equivalent ContainsCharacters method for matching name and clarity and remove IsCharacterString in 7.0? Thoughts?

@Sergio0694 curious on your optimization expertise for these cases? (especially as these aren't really localized as well... though larger changes to the behavior should probably go in the 7.0 release)

@michael-hawker If the regex is just that, you can replace that with a linear search and a comparison of the value of each character, to make sure it's within the valid range. [A-Z] in Unicode corresponds to the [65, 90] range, and [a-z] is [97, 122]. The method can be implemented like so:

public static bool ContainsCharactersOrWhiteSpace(string text)
{
    foreach (char c in text)
    {
        if (c == ' ' ||
            unchecked((uint)c - 'A') <= 'Z' ||
            unchecked((uint)c - 'a') <= 'z')
        {
            return false;
        }
    }

    return true;
}

I'm using unchecked for clarity to indicate that the subtraction will intentionally overflow when the value is lower than the minimu, which we use to skip a conditional branch. Basically:

  • If c was lower than 'A', it will overflow and become greater than 'A', so the check will fail.
  • If it was at least 'A', it will align to that letter. Then we can use a single conditional branch to verify that the character was indeed in the intended range ([A-Z]).

This trick lets us skip two conditional branches, as we can check each range with a single branch. This is a similar optimization to one of those added to .NET 5 (you can read the blog post here).

That foreach loop is already optimized by the JIT (and the .NET Native compiler) to be converted into a for loop with no bounds checks on each position, so the method doesn't do any allocation. This would be much faster than the regex one, though it's a bit less flexible tha a regex.

Hope this helps! 馃槃

Thanks @Sergio0694, figured you'd have some great tips. Think it could be a good solution for now, but am still curious on the localization bit and the meaning of 'character'. Would be interested to know what some other folks think about these types of methods.

It'd be good to hear some scenarios of where this is being used. @AbdAlghaniAlbiek can you share your scenario with us? What are you trying to accomplish?

  string[] subTxtName = txtName.Text.Split(" ");
            for (int i = 0; i < subTxtName.Length; i++)
            {
               //Check if this item is space or tab
                if (subTxtName[i] != "")
                {
                    //Isn't space or tab -> Check if It is Unvalid Name format
                    if (!subTxtName[i].IsCharacterString())
                    {
                        return false;
                    }
                }
            }

In my way I split the text that I want to test it to List of strings and check each subtext
It isn't ideal solution but It's works 馃槄馃槄

Was this page helpful?
0 / 5 - 0 ratings