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

Fix Stop Index Check in strSubstring Function for Accurate Substring Extraction! ( Update functions.go ) #2794

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Commits on Jan 11, 2024

  1. Update functions.go

    **Issue Description:**
    The current `strSubstring` function has a potential issue with the stop index check. The condition `if stop >= len(str)` may result in undesired behavior, especially when attempting to include the last character in the substring. Since the slicing operation `v = str[start : stop+1]` is left-closed and right-open, the character at the `stop` index is excluded.
    
    **Proposed Solution:**
    To address this, it is suggested to modify the condition to `if stop > len(str) || stop < 0` for a more accurate check on invalid stop indices. Additionally, the error message should be updated to reflect this change:
    
    ```go
    if stop > len(str) || stop < 0 {
        return nil, fmt.Errorf("invalid stop index for string in strSubstring: %d", stop)
    }
    v = str[start : stop+1]
    ```
    
    This modification ensures correct substring extraction, even when attempting to include the character at the stop index.
    lichenliang666 committed Jan 11, 2024
    Configuration menu
    Copy the full SHA
    2175b88 View commit details
    Browse the repository at this point in the history
  2. Update functions.go

    lichenliang666 committed Jan 11, 2024
    Configuration menu
    Copy the full SHA
    ae67ee1 View commit details
    Browse the repository at this point in the history