Re: mbstring, a proposition of additional functions

From: Date: Mon, 24 Sep 2012 14:41:30 +0000
Subject: Re: mbstring, a proposition of additional functions
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-63227@lists.php.net to get a copy of this message
Hi, I sent this email during this summer and nobody replied. I would like to know your opinion. On 06/08/12 21:54, Ivan Enderlin @ Hoa wrote:
Hello, ext/mbstring is very useful, but from my point of view, some functions are missing. I would like to propose the addition of the following functions. mb_append($str, $piece) and mb_prepend($str, $piece): To add a $piece to, respectively, the end and the start of $string. It should consider the text direction, e.g. if $str is Right-to-Left, mb_append() will add $piece to the end, so the left, of $string. mb_concat($str, $piece): If mb_append() and mb_prepend() are too hard to understand by users, mb_concat() would be pretty much simpler and will act in the same way. I would insert:
mb_replace($str, $search, $replace): Complementary to str_replace and less simpler than preg_replace or preg_filter (and with better performance because it does not need to compile regex to automata etc.).
mb_pad($length, $piece, $end): Pretty much like strpad but with multi-bytes considering. Note that, like mb_append and mb_prepend, we do not speak about left and right, but start and end. mb_get_direction($str): Return LTR or RTL. For one char, it is “easy”. For a string, it is more complex because we can have embedding directions (a string with many directions). mb_get_char($code) or mb_chr($code): Get Unicode character from a code-point/decimal value representation. mb_get_code($char) or mb_ord($char): Get code-point/decimal value representation from of Unicode character. Finally, it would be great if mb_substr() could consider text direction (or create a new dedicated function?). For example, mb_substr($str, 0, 1) will return the first char from the start (and not the left as it is implemented now). I think it could help developers to create nice libraries without requiring a lot of skills in Unicode. Thoughts? Best regards :-).
Recently, I crafted a String object that support most of the propositions written here. Please, see https://github.com/hoaproject/String (file String.php). The code is very simple (it implies that it do not need a lot of work to implement it in PHP I hope). Some interesting methods:
    • append;
    • prepend;
    • pad;
    • getIterator (equivalent of str_split);
    • \ArrayAccess methods (with support of out-of-bound indexes);
    • getByteAt (with support of out-of-bound indexes);
    • getCharDirection;
    • fromCode;
    • toCode.
Hope this helps :-), Best regards. -- Ivan Enderlin Developer of Hoa http://hoa.42/ or http://hoa-project.net/ PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis) http://disc.univ-fcomte.fr/ and http://www.inria.fr/ Member of HTML and WebApps Working Group of W3C http://w3.org/

« previous php.internals (#63227) next »