kphp icon indicating copy to clipboard operation
kphp copied to clipboard

Add mbstring functions to kphp

Open andarut opened this issue 1 year ago • 0 comments

What is mbstring

While there are many languages in which every necessary character can be represented by a one-to-one mapping to an 8-bit value, there are also several languages which require so many characters for written communication that they cannot be contained within the range a mere byte can code (A byte is made up of eight bits. Each bit can contain only two distinct values, one or zero. Because of this, a byte can only represent 256 unique values (two to the power of eight)). Multibyte character encoding schemes were developed to express more than 256 characters in the regular bytewise coding system.

When you manipulate (trim, split, splice, etc.) strings encoded in a multibyte encoding, you need to use special functions since two or more consecutive bytes may represent a single character in such encoding schemes. Otherwise, if you apply a non-multibyte-aware string function to the string, it probably fails to detect the beginning or ending of the multibyte character and ends up with a corrupted garbage string that most likely loses its original meaning.

mbstring provides multibyte specific string functions that help you deal with multibyte encodings in PHP. In addition to that, mbstring handles character encoding conversion between the possible encoding pairs. mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience. (source)

How it works

PHP uses a custom version of the libmbfl library to implement mbstring. I forked libmbfl to change the build process, so KPHP can build and link it inside without installing anything.

libmbfl is a C library, so i merged this PR to my previous PR where i fixed building C libraries inside KPHP.

Installing

In PHP mbstring is an extension, so to use it you need to specify cmake flag --enable-mbstring when building. I did something similar - to use mbstring in KPHP you need to specify cmake flag -DMBFL=On when building.

Runnings tests

ctest -j$(nproc) | grep mbstring

TODO

  • [x] mb_convert_encoding
  • [x] mb_check_encoding
  • [x] transfer MBFL flag to runtime (from external libs)
  • [x] restore functions only for UTF-8 and Windows-1251 encodings
  • [x] declarations for all mbstring functions
  • [ ] mb_chr
  • [ ] mb_convert_case
  • [ ] mb_convert_kana
  • [ ] mb_convert_variables
  • [ ] mb_decode_numericentity
  • [ ] mb_detect_encoding
  • [ ] mb_detect_order
  • [ ] mb_encode_mimeheader
  • [ ] mb_encode_numericentity
  • [ ] mb_encoding_aliases
  • [ ] mb_ereg_match
  • [ ] mb_ereg_replace_callback
  • [ ] mb_ereg_replace
  • [ ] mb_ereg_search_getpos
  • [ ] mb_ereg_search_getregs
  • [ ] mb_ereg_search_init
  • [ ] mb_ereg_search_pos
  • [ ] mb_ereg_search_regs
  • [ ] mb_ereg_search_setpos
  • [ ] mb_ereg_search
  • [ ] mb_ereg
  • [ ] mb_eregi_replace
  • [ ] mb_eregi
  • [ ] mb_get_info
  • [ ] mb_http_input
  • [ ] mb_http_output
  • [ ] mb_internal_encoding
  • [ ] mb_language
  • [ ] mb_list_encodings
  • [ ] mb_ord
  • [ ] mb_output_handler
  • [ ] mb_parse_str
  • [ ] mb_preferred_mime_name
  • [ ] mb_regex_encoding
  • [ ] mb_regex_set_options
  • [ ] mb_scrub
  • [ ] mb_send_mail
  • [ ] mb_split
  • [ ] mb_str_split
  • [ ] mb_strcut
  • [ ] mb_strimwidth
  • [ ] mb_stripos
  • [ ] mb_stristr
  • [ ] mb_strlen
  • [ ] mb_strpos
  • [ ] mb_strrchr
  • [ ] mb_strrichr
  • [ ] mb_strripos
  • [ ] mb_strrpos
  • [ ] mb_strstr
  • [ ] mb_strtolower
  • [ ] mb_strtoupper
  • [ ] mb_strwidth
  • [ ] mb_substitute_character
  • [ ] mb_substr_count
  • [ ] mb_substr
  • [ ] mb_send_mail

andarut avatar Apr 20 '23 01:04 andarut