xref: /webtrees/app/Encodings/UTF16LE.php (revision d11be7027e34e3121be11cc025421873364403f9)
11c6adce8SGreg Roach<?php
21c6adce8SGreg Roach
31c6adce8SGreg Roach/**
41c6adce8SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
61c6adce8SGreg Roach * This program is free software: you can redistribute it and/or modify
71c6adce8SGreg Roach * it under the terms of the GNU General Public License as published by
81c6adce8SGreg Roach * the Free Software Foundation, either version 3 of the License, or
91c6adce8SGreg Roach * (at your option) any later version.
101c6adce8SGreg Roach * This program is distributed in the hope that it will be useful,
111c6adce8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121c6adce8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131c6adce8SGreg Roach * GNU General Public License for more details.
141c6adce8SGreg Roach * You should have received a copy of the GNU General Public License
151c6adce8SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
161c6adce8SGreg Roach */
171c6adce8SGreg Roach
181c6adce8SGreg Roachdeclare(strict_types=1);
191c6adce8SGreg Roach
201c6adce8SGreg Roachnamespace Fisharebest\Webtrees\Encodings;
211c6adce8SGreg Roach
221c6adce8SGreg Roachuse function chr;
231c6adce8SGreg Roachuse function intdiv;
241c6adce8SGreg Roachuse function ord;
251c6adce8SGreg Roach
261c6adce8SGreg Roach/**
271c6adce8SGreg Roach * Convert between UTF-16LE and UTF-8.
281c6adce8SGreg Roach */
291c6adce8SGreg Roachclass UTF16LE extends AbstractUTF16Encoding
301c6adce8SGreg Roach{
311c6adce8SGreg Roach    public const NAME = 'UTF-16LE';
321c6adce8SGreg Roach
331c6adce8SGreg Roach    public const BYTE_ORDER_MARK       = "\xFF\xFE";
341c6adce8SGreg Roach    public const REPLACEMENT_CHARACTER = "\xFD\xFF";
351c6adce8SGreg Roach
361c6adce8SGreg Roach    /**
371c6adce8SGreg Roach     * Convert two bytes to a code-point, taking care of byte-order.
381c6adce8SGreg Roach     *
391c6adce8SGreg Roach     * @param string $character
401c6adce8SGreg Roach     *
411c6adce8SGreg Roach     * @return int
421c6adce8SGreg Roach     */
431c6adce8SGreg Roach    protected function characterToCodePoint(string $character): int
441c6adce8SGreg Roach    {
451c6adce8SGreg Roach        return ord($character[0]) + 256 * ord($character[1]);
461c6adce8SGreg Roach    }
471c6adce8SGreg Roach
481c6adce8SGreg Roach    /**
491c6adce8SGreg Roach     * Convert a code-point to two bytes, taking care of byte-order.
501c6adce8SGreg Roach     *
511c6adce8SGreg Roach     * @param int $code_point
521c6adce8SGreg Roach     *
531c6adce8SGreg Roach     * @return string
541c6adce8SGreg Roach     */
551c6adce8SGreg Roach    protected function codePointToCharacter(int $code_point): string
561c6adce8SGreg Roach    {
571c6adce8SGreg Roach        if ($code_point >= 0xD800 && $code_point <= 0xDFFF) {
581c6adce8SGreg Roach            return self::REPLACEMENT_CHARACTER;
591c6adce8SGreg Roach        }
601c6adce8SGreg Roach
611c6adce8SGreg Roach        return chr($code_point % 256) . chr(intdiv($code_point, 256));
621c6adce8SGreg Roach    }
631c6adce8SGreg Roach}
64