Skip to content

Conversation

@sumaiazaman
Copy link
Contributor

@sumaiazaman sumaiazaman commented Nov 24, 2025

This PR adds an early return optimization to the Str::unwrap() method when both wrappers are empty strings.

Changes:

  • Added early return check when both $before and $after are empty
  • Added check to skip startsWith() when $before is empty
  • Avoids unnecessary function calls for edge cases
  • Improves performance and maintains consistency with Str::start() and Str::finish()

Before:

public static function unwrap($value, $before, $after = null)
{
    if (static::startsWith($value, $before)) {
        $value = static::substr($value, static::length($before));
    }

    if (static::endsWith($value, $after ??= $before)) {
        $value = static::substr($value, 0, -static::length($after));
    }

    return $value;
}

After:

public static function unwrap($value, $before, $after = null)
{
    if ($before === '' && ($after === '' || $after === null)) {
        return $value;
    }

    if ($before !== '' && static::startsWith($value, $before)) {
        $value = static::substr($value, static::length($before));
    }

    if (static::endsWith($value, $after ??= $before)) {
        $value = static::substr($value, 0, -static::length($after));
    }

    return $value;
}

@sumaiazaman sumaiazaman changed the title Add early return to Str::unwrap() for empty wrappers [12.x] Add early return to Str::unwrap() for empty wrappers Nov 24, 2025
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