-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added String.count method #25090
Added String.count method #25090
Conversation
Shouldn't |
I think you should use a regular expression for that kind of precision :) However you right - TestTest exist only one time if you looks on it from the one side, but 2 times from another |
Also, In Python it will be 1, you can test it on https://repl.it/languages/python3
|
I agree that returning overlapping results would not be what would be expected in most cases. |
This should have a corresponding case-insensitive |
Added countn |
I think the function should support negative indices if possible. Also, default value for p_from should be 0, not -1. |
I dont think it really need for count + it will complicate the code. rcount and rcountn could be implemented for that in the future, but for now I want to implement it in simple form.
Changed |
I guess I need to write a C# version for this. |
Added C# version, cc @neikeq |
953c259
to
e695b60
Compare
// <summary> | ||
// Return the amount of substrings in string. | ||
// </summary> | ||
public static int Count(this string instance, string what, bool ignoreCase = false, int from = 0, int to = 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid double negatives. Instead of "ignoreCase", it should be "caseSensitive". Do a Ctrl+F in this file for bool caseSensitive = true
to see examples of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's pretty common for case-sensitive-by-default APIs to have a flag for ignore case, not for enable case sensitivity (which is default). See e.g. any kind of grepping tool with -i
flag.
ignoreCase = false
is not a double negative, dontIgnoreCase = true
would be.
That being said, if other extensions in this case favor caseSensitive = true
, I'm not against that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I changed to caseSensitive because the other functions in that file is using it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO "ignore" is itself a negative, because it means "don't care about it", so ignoreCase = false
would be "don't not care about case".
In C#, we specify in the method declaration which one is the default with true or false, so we don't need to depend on a flag existing or not - whichever makes the most readable code is best.
However, if this was changed to ignoreCase
, it should be done for all of the other methods too.
Code seems good to me so it could be merged, but I think it should likely be added to GDNative too? If so, I believe it would have to be added to the 1.2 extension (CC @karroffel to confirm). |
This could be added to GDNative, yes. If there is an issue about things that should be added to the 1.2 core extension then it could be mentioned there and doesn't need to block this PR. |
@karroffel last time when I tried to push to GDNative - #27452 you said it will break the binary compatibility.. what exactly I must do ? |
The problem was that new code was added to a stabilized extension. Right now the GDNative core extension 1.2 (I think?) is not stabilized yet, so the code could be added there. So for example the 1.0 GDNative core must not be touched, but 1.2 can be touched as nobody is expected to already depend on it. |
Ok, so I've added a GDNative code similar to what I did in #27452.. Please check @karroffel |
Not quite :) The new things should be added here: https://github.com/godotengine/godot/blob/89dfcbc6f7f57db4cd8ce3daa10043b40c0b5231/modules/gdnative/gdnative_api.json#L47 |
And now ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GDNative seems okay!
2ce6988
to
b3247b9
Compare
I've fixed documentation a bit - I guess mention "default" for parameters is not neccessary now (these values is clearly visible in editor docs). |
Thanks! |
This method returns the amount of identical substrings in string, Its particularly useful for text parsing purposes + there is simular function in Python API and in the Array class of Godot
example
Fix #27749