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

add findLast string method #723

Merged
merged 7 commits into from
Jan 2, 2024

Conversation

briandowns
Copy link
Contributor

What's Changed:

Add lastIndexOf string method to find the last index of a given string in another string.

Type of Change:

  • Bug fix
  • New feature
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Housekeeping:

  • Tests have been updated to reflect the changes done within this PR (if applicable).
  • Documentation has been updated to reflect the changes done within this PR (if applicable).

Screenshots (If Applicable):

Signed-off-by: Brian Downs <[email protected]>
@briandowns briandowns self-assigned this Jan 1, 2024
@Jason2605
Copy link
Member

Thanks for this! Would it be possible to maybe change the name to findLast? Just thinking to keep it consistent with the forward looking .find(). Thinking as well (potentially a later PR) if we also want the optional skip like we have with .find()

Signed-off-by: Brian Downs <[email protected]>
@briandowns
Copy link
Contributor Author

Thanks for the review. Changes made. I can do the optional skip in another PR.

Signed-off-by: Brian Downs <[email protected]>
Copy link
Member

@Jason2605 Jason2605 left a comment

Choose a reason for hiding this comment

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

Couple of small comments!

src/vm/datatypes/strings.c Outdated Show resolved Hide resolved
@@ -199,6 +199,15 @@ Returns true if a string contains another string.

To find the index of a given substring, use the `.find()` method. This method takes an optional second parameter which can be used to skip the first `n` number of appearances of the substring. This method returns `-1` if the substring could not be found. Otherwise, it returns the index of the string.

```cs
Copy link
Member

Choose a reason for hiding this comment

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

I think there's a little bit of a mix up with the docs here, the find examples have went under .findLast and the findLast have went under .find

Comment on lines 203 to 204
"woolly woolly mammoth".lastIndexOf("woolly"); // 7
"mammoth".lastIndexOf("woolly"); // -1
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"woolly woolly mammoth".lastIndexOf("woolly"); // 7
"mammoth".lastIndexOf("woolly"); // -1
"woolly woolly mammoth".findLast("woolly"); // 7
"mammoth".findLast("woolly"); // -1

@@ -0,0 +1,17 @@
/**
* lastIndexOf.du
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* lastIndexOf.du
* findLast.du

/**
* lastIndexOf.du
*
* Testing the str.lastIndexOf() method
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Testing the str.lastIndexOf() method
* Testing the str.findLast() method

*
* Testing the str.lastIndexOf() method
*
* .lastIndexOf() returns the last index of the given string
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* .lastIndexOf() returns the last index of the given string
* .findLast() returns the last index of the given string

@Jason2605 Jason2605 changed the title add lastIndexOf string method add findLast string method Jan 2, 2024
@Jason2605
Copy link
Member

Thanks for this!

@briandowns
Copy link
Contributor Author

Thanks for that commit. Apparently my editor's find and replace isn't quite working as expected. I needed this method for a text wrap module I wrote over the weekend. It's super small in Dictu but a pain to do it in C. Do we want to entertain string methods in Dictu like we have with lists and dicts?

@Jason2605
Copy link
Member

No worries! Yeah built in methods on strings are more than welcome additions

@briandowns
Copy link
Contributor Author

Awesome. Thanks!

@Jason2605 Jason2605 merged commit 023940e into dictu-lang:develop Jan 2, 2024
8 checks passed
@Jason2605 Jason2605 mentioned this pull request Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants