Skip to content

Stdlib: mb_strlen() — encoding argument + error behavior parity with php-src ext/mbstring #4405

Description

@PurHur

Category

stdlib

Problem

mb_strlen() exists but has compiler-specific constraints (UTF-8/ASCII/8BIT-only in JIT path; other encodings depend on host mbstring, and error handling differs). For parity and portability we need php-src-consistent behavior for:

  • accepted values for $encoding (string, null, omitted)
  • invalid encoding handling (warnings / ValueError depending on PHP version)
  • interaction with internal encoding (when $encoding omitted)
  • return value for empty string and multibyte characters

php-src reference

  • ext/mbstring/mbstring.c — PHP_FUNCTION(mb_strlen)
  • ext/mbstring/php_mbstring.h — encoding tables and option parsing
  • PHPTs: ext/mbstring/tests/mb_strlen*.phpt

Repro (today)

<?php
echo mb_strlen('é', 'UTF-8'), "\n";

// Zend accepts null to mean "use internal encoding".
try {
    var_dump(mb_strlen('hello', null));
} catch (Throwable $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

// Invalid encoding should match Zend (warning/ValueError + return false).
try {
    var_dump(mb_strlen('hello', 'NO_SUCH_ENCODING'));
} catch (Throwable $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
php repro.php   # Zend reference (with ext/mbstring)

Scope (this repo)

  • ext/types/mb_strlen.php + ext/types/JitMbStrlen.php + native helpers under lib/AOT/runtime/
  • Decide parity policy for environments without host mbstring (Docker image vs harness host)
  • Add/port compliance PHPTs for encoding parsing + error behavior

Done when

  • $encoding accepts null/omitted like Zend and follows internal encoding rules
  • Invalid encoding behavior matches php-src (exception vs warning + false) for targeted PHP version
  • JIT/AOT/VM behavior aligned for UTF-8 and ASCII; documented/guarded behavior for other encodings

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions