2004-08-29 10:30:23 +00:00
|
|
|
<?php
|
2010-08-15 07:47:23 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Unicode normalization routines
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2004 Brion Vibber <brion@pobox.com>
|
2014-03-13 22:23:56 +00:00
|
|
|
* https://www.mediawiki.org/
|
2011-03-27 12:21:45 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup UtfNormal
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
|
2011-03-27 12:21:45 +00:00
|
|
|
/**
|
|
|
|
|
* @defgroup UtfNormal UtfNormal
|
|
|
|
|
*/
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
|
2015-03-07 09:27:42 +00:00
|
|
|
use UtfNormal\Validator;
|
2004-10-07 05:59:10 +00:00
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Unicode normalization routines for working with UTF-8 strings.
|
|
|
|
|
* Currently assumes that input strings are valid UTF-8!
|
|
|
|
|
*
|
|
|
|
|
* Not as fast as I'd like, but should be usable for most purposes.
|
|
|
|
|
* UtfNormal::toNFC() will bail early if given ASCII text or text
|
2013-03-13 07:42:41 +00:00
|
|
|
* it can quickly determine is already normalized.
|
2011-03-27 12:21:45 +00:00
|
|
|
*
|
|
|
|
|
* All functions can be called static.
|
|
|
|
|
*
|
|
|
|
|
* See description of forms at http://www.unicode.org/reports/tr15/
|
|
|
|
|
*
|
2015-03-07 09:27:42 +00:00
|
|
|
* @deprecated since 1.25, use UtfNormal\Validator directly
|
2011-03-27 12:21:45 +00:00
|
|
|
* @ingroup UtfNormal
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
class UtfNormal {
|
2011-03-27 12:21:45 +00:00
|
|
|
/**
|
|
|
|
|
* The ultimate convenience function! Clean up invalid UTF-8 sequences,
|
|
|
|
|
* and convert to normal form C, canonical composition.
|
|
|
|
|
*
|
|
|
|
|
* Fast return for pure ASCII strings; some lesser optimizations for
|
|
|
|
|
* strings containing only known-good characters. Not as fast as toNFC().
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a UTF-8 string
|
2011-03-27 12:21:45 +00:00
|
|
|
* @return string a clean, shiny, normalized UTF-8 string
|
|
|
|
|
*/
|
|
|
|
|
static function cleanUp( $string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::cleanUp( $string );
|
2004-09-03 05:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-27 12:21:45 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a UTF-8 string to normal form C, canonical composition.
|
|
|
|
|
* Fast return for pure ASCII strings; some lesser optimizations for
|
|
|
|
|
* strings containing only known-good characters.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
2011-03-27 12:21:45 +00:00
|
|
|
* @return string a UTF-8 string in normal form C
|
|
|
|
|
*/
|
|
|
|
|
static function toNFC( $string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::toNFC( $string );
|
2011-03-24 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Convert a UTF-8 string to normal form D, canonical decomposition.
|
|
|
|
|
* Fast return for pure ASCII strings.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
2011-03-27 12:21:45 +00:00
|
|
|
* @return string a UTF-8 string in normal form D
|
|
|
|
|
*/
|
|
|
|
|
static function toNFD( $string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::toNFD( $string );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Convert a UTF-8 string to normal form KC, compatibility composition.
|
|
|
|
|
* This may cause irreversible information loss, use judiciously.
|
|
|
|
|
* Fast return for pure ASCII strings.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
2011-03-27 12:21:45 +00:00
|
|
|
* @return string a UTF-8 string in normal form KC
|
|
|
|
|
*/
|
|
|
|
|
static function toNFKC( $string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::toNFKC( $string );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Convert a UTF-8 string to normal form KD, compatibility decomposition.
|
|
|
|
|
* This may cause irreversible information loss, use judiciously.
|
|
|
|
|
* Fast return for pure ASCII strings.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
2011-03-27 12:21:45 +00:00
|
|
|
* @return string a UTF-8 string in normal form KD
|
|
|
|
|
*/
|
|
|
|
|
static function toNFKD( $string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::toNFKD( $string );
|
2004-10-09 08:08:26 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Returns true if the string is _definitely_ in NFC.
|
|
|
|
|
* Returns false if not or uncertain.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
2011-03-27 12:21:45 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
static function quickIsNFC( $string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::quickIsNFC( $string );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
2004-09-03 05:39:30 +00:00
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
2011-03-27 12:21:45 +00:00
|
|
|
* Returns true if the string is _definitely_ in NFC.
|
|
|
|
|
* Returns false if not or uncertain.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $string a UTF-8 string, altered on output to be valid UTF-8 safe for XML.
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return bool
|
2011-03-27 12:21:45 +00:00
|
|
|
*/
|
|
|
|
|
static function quickIsNFCVerify( &$string ) {
|
2015-03-07 09:27:42 +00:00
|
|
|
return Validator::quickIsNFCVerify( $string );
|
2011-04-16 15:32:19 +00:00
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|