Skip to content
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

Merged
merged 1 commit into from
Jul 24, 2019
Merged

Conversation

Chaosus
Copy link
Member

@Chaosus Chaosus commented Jan 18, 2019

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

	print(str("".count("Test"))) # 0
	print(str("Test".count(""))) # 0
	print(str("Test".count("test"))) # 0
	print(str("Test".count("TEST"))) # 0
	print(str("TEST".count("TEST"))) # 1
	print(str("Test".count("Test"))) # 1
	print(str("aTest".count("Test"))) # 1
	print(str("Testa".count("Test"))) # 1
	print(str("TestTestTest".count("Test"))) # 3
	print(str("TestTestTest".count("TestTest"))) # 1
	print(str("TestGodotTestGodotTestGodot".count("Test"))) # 3
	
	print(str("TestTestTestTest".count("Test", 4, 8))) # 1
	print(str("TestTestTestTest".count("Test", 4, 12))) # 2
	print(str("TestTestTestTest".count("Test", 4, 16))) # 3
	print(str("TestTestTestTest".count("Test", 4))) # 3

Fix #27749

@escargot-sans-gluten
Copy link

Shouldn't "TestTestTest".count("TestTest") return 2? ("TestTest Test" and "Test TestTest") Is this behavior unwanted?

@Chaosus
Copy link
Member Author

Chaosus commented Jan 18, 2019

Shouldn't "TestTestTest".count("TestTest") return 2?

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

@Chaosus
Copy link
Member Author

Chaosus commented Jan 18, 2019

Also, In Python it will be 1, you can test it on https://repl.it/languages/python3

s = "TestTestTest"
print(s.count("TestTest",0,12))

@hpvb
Copy link
Member

hpvb commented Jan 24, 2019

I agree that returning overlapping results would not be what would be expected in most cases.

@aaronfranke
Copy link
Member

This should have a corresponding case-insensitive countn similar to matchn, replacen, and rfindn

@Chaosus Chaosus requested a review from a team as a code owner January 29, 2019 07:43
@Chaosus
Copy link
Member Author

Chaosus commented Jan 29, 2019

Added countn

@bojidar-bg
Copy link
Contributor

I think the function should support negative indices if possible. Also, default value for p_from should be 0, not -1.

@Chaosus
Copy link
Member Author

Chaosus commented Jan 29, 2019

I think the function should support negative indices if possible.

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.

Also, default value for p_from should be 0, not -1.

Changed

@Chaosus
Copy link
Member Author

Chaosus commented Apr 7, 2019

I guess I need to write a C# version for this.

@Chaosus Chaosus requested a review from neikeq as a code owner April 7, 2019 12:09
@Chaosus
Copy link
Member Author

Chaosus commented Apr 7, 2019

Added C# version, cc @neikeq

@Chaosus Chaosus force-pushed the string_count branch 2 times, most recently from 953c259 to e695b60 Compare April 8, 2019 05:33
// <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)
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

@aaronfranke aaronfranke Apr 8, 2019

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.

doc/classes/String.xml Outdated Show resolved Hide resolved
@akien-mga
Copy link
Member

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).

core/ustring.cpp Show resolved Hide resolved
@karroffel
Copy link
Contributor

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.

@Chaosus
Copy link
Member Author

Chaosus commented Jul 23, 2019

@karroffel last time when I tried to push to GDNative - #27452 you said it will break the binary compatibility.. what exactly I must do ?

@karroffel
Copy link
Contributor

@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.

@Chaosus
Copy link
Member Author

Chaosus commented Jul 23, 2019

Ok, so I've added a GDNative code similar to what I did in #27452.. Please check @karroffel

@karroffel
Copy link
Contributor

@Chaosus
Copy link
Member Author

Chaosus commented Jul 23, 2019

And now ?

Copy link
Contributor

@karroffel karroffel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GDNative seems okay!

core/ustring.cpp Show resolved Hide resolved
@Chaosus Chaosus force-pushed the string_count branch 2 times, most recently from 2ce6988 to b3247b9 Compare July 23, 2019 15:22
@Chaosus
Copy link
Member Author

Chaosus commented Jul 23, 2019

I've fixed documentation a bit - I guess mention "default" for parameters is not neccessary now (these values is clearly visible in editor docs).

@akien-mga akien-mga merged commit aa062c5 into godotengine:master Jul 24, 2019
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String.count() method to count occurrences of substring
9 participants