File indexing completed on 2024-05-05 17:57:59

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "jisx0201charcodec.hpp"
0010 
0011 // lib
0012 #include <character.hpp>
0013 // Qt
0014 #include <QString>
0015 
0016 namespace Okteta {
0017 
0018 bool JISX0201CharCodec::encode(Byte* byte, const QChar& _char) const
0019 {
0020     const ushort charUnicode = _char.unicode();
0021 
0022     constexpr unsigned char unmatched = 0xFF; // value not in target set
0023     const unsigned char _byte =
0024         (charUnicode <= 0x005B) ?                          charUnicode :
0025         (0x005D <= charUnicode && charUnicode <= 0x007D) ? charUnicode :
0026         (charUnicode == 0x00A5) ?                          92 :
0027         (charUnicode == 0x203E) ?                          126 :
0028         (0xFF61 <= charUnicode && charUnicode <= 0xFF9F) ? (charUnicode - 0xFF61 + 161) :
0029         /* else */                                         unmatched;
0030 
0031     // not covered?
0032     if (_byte == unmatched) {
0033         return false;
0034     }
0035 
0036     *byte = _byte;
0037 
0038     return true;
0039 }
0040 
0041 Character JISX0201CharCodec::decode(Byte byte) const
0042 {
0043     const ushort unicode =
0044         (byte <= 91) ||
0045         (93 <= byte && byte <= 125)
0046             ?  ushort(byte) :
0047         (92 == byte)
0048             ?  ushort(0x00A5) :
0049         (126 == byte)
0050             ?  ushort(0x203E) :
0051         (161 <= byte && byte <= 223)
0052             ? ushort(0xFF61 - 161 + byte) :
0053         /* else */ 0xFFFD;
0054 
0055     if (unicode == 0xFFFD) {
0056         return {QChar(0), true};
0057     }
0058 
0059     return {QChar(unicode)};
0060 }
0061 
0062 bool JISX0201CharCodec::canEncode(const QChar& _char) const
0063 {
0064     const ushort charUnicode = _char.unicode();
0065     return
0066         (charUnicode <= 0x005B) ||
0067         (0x005D <= charUnicode && charUnicode <= 0x007D) ||
0068         (charUnicode == 0x00A5) || (charUnicode == 0x203E) ||
0069         (0xFF61 <= charUnicode && charUnicode <= 0xFF9F);
0070 }
0071 
0072 const QString& JISX0201CharCodec::name() const
0073 {
0074     return codecName();
0075 }
0076 
0077 const QString& JISX0201CharCodec::codecName()
0078 {
0079     static const QString name = QStringLiteral("JIS X 0201");
0080     return name;
0081 }
0082 
0083 }