Apply clang-format to code base

This commit is contained in:
clang-format-bot 2022-09-22 21:26:05 +03:00 committed by ζeh Matt
parent f37d0be806
commit ddb0522bbf
No known key found for this signature in database
GPG key ID: 18CE582C71A225B0
2199 changed files with 118692 additions and 114392 deletions

View file

@ -2,116 +2,123 @@
#include <iostream>
#include <iconv.h>
#include <cassert>
#include <iconv.h>
void tab() { std::cout << " "; }
void tab()
{
std::cout << " ";
}
// write one number with a space in front of it and a comma after it
void num(char i, bool last)
{
// Convert i to its integer value, i.e. -128 to 127. Printing it directly
// would result in non-printable characters in the source code, which is bad.
std::cout << " " << static_cast<int>(i);
if(!last) std::cout << ",";
// Convert i to its integer value, i.e. -128 to 127. Printing it directly
// would result in non-printable characters in the source code, which is bad.
std::cout << " " << static_cast<int>(i);
if (!last)
std::cout << ",";
}
// Write one table entry (UTF8 value), 1-5 bytes
void writeChar(char *value, int length, bool last, const std::string &comment="")
void writeChar(char* value, int length, bool last, const std::string& comment = "")
{
assert(length >= 1 && length <= 5);
tab();
num(length, false);
for(int i=0;i<5;i++)
num(value[i], last && i==4);
assert(length >= 1 && length <= 5);
tab();
num(length, false);
for (int i = 0; i < 5; i++)
num(value[i], last && i == 4);
if(comment != "")
std::cout << " // " << comment;
if (comment != "")
std::cout << " // " << comment;
std::cout << std::endl;
std::cout << std::endl;
}
// What to write on missing characters
void writeMissing(bool last)
{
// Just write a space character
char value[5];
value[0] = ' ';
for(int i=1; i<5; i++)
value[i] = 0;
writeChar(value, 1, last, "not part of this charset");
// Just write a space character
char value[5];
value[0] = ' ';
for (int i = 1; i < 5; i++)
value[i] = 0;
writeChar(value, 1, last, "not part of this charset");
}
int write_table(const std::string &charset, const std::string &tableName)
int write_table(const std::string& charset, const std::string& tableName)
{
// Write table header
std::cout << "const static signed char " << tableName << "[] =\n{\n";
// Write table header
std::cout << "const static signed char " << tableName << "[] =\n{\n";
// Open conversion system
iconv_t cd = iconv_open ("UTF-8", charset.c_str());
// Open conversion system
iconv_t cd = iconv_open("UTF-8", charset.c_str());
// Convert each character from 0 to 255
for(int i=0; i<256; i++)
// Convert each character from 0 to 255
for (int i = 0; i < 256; i++)
{
bool last = (i==255);
bool last = (i == 255);
char input = i;
char *iptr = &input;
size_t ileft = 1;
char input = i;
char* iptr = &input;
size_t ileft = 1;
char output[5];
for(int k=0; k<5; k++) output[k] = 0;
char *optr = output;
size_t oleft = 5;
char output[5];
for (int k = 0; k < 5; k++)
output[k] = 0;
char* optr = output;
size_t oleft = 5;
size_t res = iconv(cd, &iptr, &ileft, &optr, &oleft);
size_t res = iconv(cd, &iptr, &ileft, &optr, &oleft);
if(res) writeMissing(last);
else writeChar(output, 5-oleft, last);
if (res)
writeMissing(last);
else
writeChar(output, 5 - oleft, last);
}
iconv_close (cd);
iconv_close(cd);
// Finish table
std::cout << "};\n";
// Finish table
std::cout << "};\n";
return 0;
return 0;
}
int main()
{
// Write header guard
std::cout << "#ifndef COMPONENTS_TOUTF8_TABLE_GEN_H\n#define COMPONENTS_TOUTF8_TABLE_GEN_H\n\n";
// Write header guard
std::cout << "#ifndef COMPONENTS_TOUTF8_TABLE_GEN_H\n#define COMPONENTS_TOUTF8_TABLE_GEN_H\n\n";
// Write namespace
std::cout << "namespace ToUTF8\n{\n\n";
// Write namespace
std::cout << "namespace ToUTF8\n{\n\n";
// Central European and Eastern European languages that use Latin script, such as
// Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian.
std::cout << "\n/// Central European and Eastern European languages that use Latin script,"
"\n/// such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian,"
"\n/// Serbian (Latin script), Romanian and Albanian."
"\n";
write_table("WINDOWS-1250", "windows_1250");
// Central European and Eastern European languages that use Latin script, such as
// Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian.
std::cout << "\n/// Central European and Eastern European languages that use Latin script,"
"\n/// such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian,"
"\n/// Serbian (Latin script), Romanian and Albanian."
"\n";
write_table("WINDOWS-1250", "windows_1250");
// Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages
std::cout << "\n/// Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic"
"\n/// and other languages"
"\n";
write_table("WINDOWS-1251", "windows_1251");
// Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages
std::cout << "\n/// Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic"
"\n/// and other languages"
"\n";
write_table("WINDOWS-1251", "windows_1251");
// English
std::cout << "\n/// Latin alphabet used by English and some other Western languages"
"\n";
write_table("WINDOWS-1252", "windows_1252");
// English
std::cout << "\n/// Latin alphabet used by English and some other Western languages"
"\n";
write_table("WINDOWS-1252", "windows_1252");
write_table("CP437", "cp437");
write_table("CP437", "cp437");
// Close namespace
std::cout << "\n}\n\n";
// Close namespace
std::cout << "\n}\n\n";
// Close header guard
std::cout << "#endif\n\n";
// Close header guard
std::cout << "#endif\n\n";
return 0;
return 0;
}

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,9 @@
#include "to_utf8.hpp"
#include <vector>
#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <components/debug/debuglog.hpp>
@ -39,7 +39,6 @@
even in the cases where some conversion is necessary.
*/
// Generated tables
#include "tables_gen.hpp"
@ -49,7 +48,7 @@ namespace
{
std::string_view::iterator skipAscii(std::string_view input)
{
return std::find_if(input.begin(), input.end(), [] (unsigned char v) { return v == 0 || v >= 128; });
return std::find_if(input.begin(), input.end(), [](unsigned char v) { return v == 0 || v >= 128; });
}
std::basic_string_view<signed char> getTranslationArray(FromType sourceEncoding)
@ -57,13 +56,13 @@ namespace
switch (sourceEncoding)
{
case ToUTF8::WINDOWS_1252:
return {ToUTF8::windows_1252, std::size(ToUTF8::windows_1252)};
return { ToUTF8::windows_1252, std::size(ToUTF8::windows_1252) };
case ToUTF8::WINDOWS_1250:
return {ToUTF8::windows_1250, std::size(ToUTF8::windows_1250)};
return { ToUTF8::windows_1250, std::size(ToUTF8::windows_1250) };
case ToUTF8::WINDOWS_1251:
return {ToUTF8::windows_1251, std::size(ToUTF8::windows_1251)};
return { ToUTF8::windows_1251, std::size(ToUTF8::windows_1251) };
case ToUTF8::CP437:
return {ToUTF8::cp437, std::size(ToUTF8::cp437)};
return { ToUTF8::cp437, std::size(ToUTF8::cp437) };
}
throw std::logic_error("Invalid source encoding: " + std::to_string(sourceEncoding));
}
@ -102,7 +101,8 @@ StatelessUtf8Encoder::StatelessUtf8Encoder(FromType sourceEncoding)
{
}
std::string_view StatelessUtf8Encoder::getUtf8(std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const
std::string_view StatelessUtf8Encoder::getUtf8(
std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const
{
if (input.empty())
return input;
@ -118,12 +118,12 @@ std::string_view StatelessUtf8Encoder::getUtf8(std::string_view input, BufferAll
const auto [outlen, ascii] = getLength(input);
// If we're pure ascii, then don't bother converting anything.
if(ascii)
if (ascii)
return std::string_view(input.data(), outlen);
// Make sure the output is large enough
resize(outlen, bufferAllocationPolicy, buffer);
char *out = buffer.data();
char* out = buffer.data();
// Translate
for (auto it = input.begin(); it != input.end() && *it != 0; ++it)
@ -139,7 +139,8 @@ std::string_view StatelessUtf8Encoder::getUtf8(std::string_view input, BufferAll
return std::string_view(buffer.data(), outlen);
}
std::string_view StatelessUtf8Encoder::getLegacyEnc(std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const
std::string_view StatelessUtf8Encoder::getLegacyEnc(
std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const
{
if (input.empty())
return input;
@ -155,12 +156,12 @@ std::string_view StatelessUtf8Encoder::getLegacyEnc(std::string_view input, Buff
const auto [outlen, ascii] = getLengthLegacyEnc(input);
// If we're pure ascii, then don't bother converting anything.
if(ascii)
if (ascii)
return std::string_view(input.data(), outlen);
// Make sure the output is large enough
resize(outlen, bufferAllocationPolicy, buffer);
char *out = buffer.data();
char* out = buffer.data();
// Translate
for (auto it = input.begin(); it != input.end() && *it != 0;)
@ -196,7 +197,7 @@ std::pair<std::size_t, bool> StatelessUtf8Encoder::getLength(std::string_view in
// were some non-ascii characters to deal with. Go to slow-mode for
// the rest of the string.
if (it == input.end() || *it == 0)
return {it - input.begin(), true};
return { it - input.begin(), true };
std::size_t len = it - input.begin();
@ -206,15 +207,14 @@ std::pair<std::size_t, bool> StatelessUtf8Encoder::getLength(std::string_view in
// lookup table.
len += mTranslationArray[static_cast<unsigned char>(*it) * 6];
++it;
}
while (it != input.end() && *it != 0);
} while (it != input.end() && *it != 0);
return {len, false};
return { len, false };
}
// Translate one character 'ch' using the translation array 'arr', and
// advance the output pointer accordingly.
void StatelessUtf8Encoder::copyFromArray(unsigned char ch, char* &out) const
void StatelessUtf8Encoder::copyFromArray(unsigned char ch, char*& out) const
{
// Optimize for ASCII values
if (ch < 128)
@ -223,7 +223,7 @@ void StatelessUtf8Encoder::copyFromArray(unsigned char ch, char* &out) const
return;
}
const signed char *in = &mTranslationArray[ch * 6];
const signed char* in = &mTranslationArray[ch * 6];
int len = *(in++);
memcpy(out, in, len);
out += len;
@ -239,7 +239,7 @@ std::pair<std::size_t, bool> StatelessUtf8Encoder::getLengthLegacyEnc(std::strin
// were some non-ascii characters to deal with. Go to slow-mode for
// the rest of the string.
if (it == input.end() || *it == 0)
return {it - input.begin(), true};
return { it - input.begin(), true };
std::size_t len = it - input.begin();
std::size_t symbolLen = 0;
@ -251,7 +251,9 @@ std::pair<std::size_t, bool> StatelessUtf8Encoder::getLengthLegacyEnc(std::strin
// lookup table.
switch (static_cast<unsigned char>(*it))
{
case 0xe2: symbolLen -= 2; break;
case 0xe2:
symbolLen -= 2;
break;
case 0xc2:
case 0xcb:
case 0xc4:
@ -260,7 +262,9 @@ std::pair<std::size_t, bool> StatelessUtf8Encoder::getLengthLegacyEnc(std::strin
case 0xd0:
case 0xd1:
case 0xd2:
case 0xc5: symbolLen -= 1; break;
case 0xc5:
symbolLen -= 1;
break;
default:
len += symbolLen;
symbolLen = 0;
@ -268,13 +272,13 @@ std::pair<std::size_t, bool> StatelessUtf8Encoder::getLengthLegacyEnc(std::strin
}
++it;
}
while (it != input.end() && *it != 0);
} while (it != input.end() && *it != 0);
return {len, false};
return { len, false };
}
void StatelessUtf8Encoder::copyFromArrayLegacyEnc(std::string_view::iterator& chp, std::string_view::iterator end, char* &out) const
void StatelessUtf8Encoder::copyFromArrayLegacyEnc(
std::string_view::iterator& chp, std::string_view::iterator end, char*& out) const
{
unsigned char ch = *(chp++);
// Optimize for ASCII values
@ -287,7 +291,9 @@ void StatelessUtf8Encoder::copyFromArrayLegacyEnc(std::string_view::iterator& ch
int len = 1;
switch (ch)
{
case 0xe2: len = 3; break;
case 0xe2:
len = 3;
break;
case 0xc2:
case 0xcb:
case 0xc4:
@ -296,7 +302,9 @@ void StatelessUtf8Encoder::copyFromArrayLegacyEnc(std::string_view::iterator& ch
case 0xd0:
case 0xd1:
case 0xd2:
case 0xc5: len = 2; break;
case 0xc5:
len = 2;
break;
}
if (len == 1) // There is no 1 length utf-8 glyph that is not 0x20 (empty space)
@ -319,7 +327,8 @@ void StatelessUtf8Encoder::copyFromArrayLegacyEnc(std::string_view::iterator& ch
for (int i = 128; i < 256; i++)
{
unsigned char b1 = mTranslationArray[i*6 + 1], b2 = mTranslationArray[i*6 + 2], b3 = mTranslationArray[i*6 + 3];
unsigned char b1 = mTranslationArray[i * 6 + 1], b2 = mTranslationArray[i * 6 + 2],
b3 = mTranslationArray[i * 6 + 3];
if (b1 == ch && b2 == ch2 && (len != 3 || b3 == ch3))
{
*(out++) = (char)i;
@ -357,7 +366,8 @@ ToUTF8::FromType ToUTF8::calculateEncoding(std::string_view encodingName)
else if (encodingName == "win1252")
return ToUTF8::WINDOWS_1252;
else
throw std::runtime_error("Unknown encoding '" + std::string(encodingName) + "', see openmw --help for available options.");
throw std::runtime_error(
"Unknown encoding '" + std::string(encodingName) + "', see openmw --help for available options.");
}
std::string ToUTF8::encodingUsingMessage(std::string_view encodingName)
@ -369,5 +379,6 @@ std::string ToUTF8::encodingUsingMessage(std::string_view encodingName)
else if (encodingName == "win1252")
return "Using default (English) font encoding.";
else
throw std::runtime_error("Unknown encoding '" + std::string(encodingName) + "', see openmw --help for available options.");
throw std::runtime_error(
"Unknown encoding '" + std::string(encodingName) + "', see openmw --help for available options.");
}

View file

@ -1,21 +1,22 @@
#ifndef COMPONENTS_TOUTF8_H
#define COMPONENTS_TOUTF8_H
#include <string>
#include <cstring>
#include <vector>
#include <string>
#include <string_view>
#include <vector>
namespace ToUTF8
{
// These are all the currently supported code pages
enum FromType
{
WINDOWS_1250, // Central ane Eastern European languages
WINDOWS_1251, // Cyrillic languages
WINDOWS_1252, // Used by English version of Morrowind (and
// probably others)
CP437 // Used for fonts (*.fnt) if data files encoding is 1252. Otherwise, uses the same encoding as the data files.
WINDOWS_1250, // Central ane Eastern European languages
WINDOWS_1251, // Cyrillic languages
WINDOWS_1252, // Used by English version of Morrowind (and
// probably others)
CP437 // Used for fonts (*.fnt) if data files encoding is 1252. Otherwise, uses the same encoding as the data
// files.
};
enum class BufferAllocationPolicy
@ -29,44 +30,47 @@ namespace ToUTF8
class StatelessUtf8Encoder
{
public:
explicit StatelessUtf8Encoder(FromType sourceEncoding);
public:
explicit StatelessUtf8Encoder(FromType sourceEncoding);
/// Convert to UTF8 from the previously given code page.
/// Returns a view to passed buffer that will be resized to fit output if it's too small.
std::string_view getUtf8(std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const;
/// Convert to UTF8 from the previously given code page.
/// Returns a view to passed buffer that will be resized to fit output if it's too small.
std::string_view getUtf8(
std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const;
/// Convert from UTF-8 to sourceEncoding.
/// Returns a view to passed buffer that will be resized to fit output if it's too small.
std::string_view getLegacyEnc(std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const;
/// Convert from UTF-8 to sourceEncoding.
/// Returns a view to passed buffer that will be resized to fit output if it's too small.
std::string_view getLegacyEnc(
std::string_view input, BufferAllocationPolicy bufferAllocationPolicy, std::string& buffer) const;
private:
inline std::pair<std::size_t, bool> getLength(std::string_view input) const;
inline void copyFromArray(unsigned char chp, char* &out) const;
inline std::pair<std::size_t, bool> getLengthLegacyEnc(std::string_view input) const;
inline void copyFromArrayLegacyEnc(std::string_view::iterator& chp, std::string_view::iterator end, char* &out) const;
private:
inline std::pair<std::size_t, bool> getLength(std::string_view input) const;
inline void copyFromArray(unsigned char chp, char*& out) const;
inline std::pair<std::size_t, bool> getLengthLegacyEnc(std::string_view input) const;
inline void copyFromArrayLegacyEnc(
std::string_view::iterator& chp, std::string_view::iterator end, char*& out) const;
const std::basic_string_view<signed char> mTranslationArray;
const std::basic_string_view<signed char> mTranslationArray;
};
class Utf8Encoder
{
public:
explicit Utf8Encoder(FromType sourceEncoding);
public:
explicit Utf8Encoder(FromType sourceEncoding);
/// Convert to UTF8 from the previously given code page.
/// Returns a view to internal buffer invalidate by next getUtf8 or getLegacyEnc call if input is not
/// ASCII-only string. Otherwise returns a view to the input.
std::string_view getUtf8(std::string_view input);
/// Convert to UTF8 from the previously given code page.
/// Returns a view to internal buffer invalidate by next getUtf8 or getLegacyEnc call if input is not
/// ASCII-only string. Otherwise returns a view to the input.
std::string_view getUtf8(std::string_view input);
/// Convert from UTF-8 to sourceEncoding.
/// Returns a view to internal buffer invalidate by next getUtf8 or getLegacyEnc call if input is not
/// ASCII-only string. Otherwise returns a view to the input.
std::string_view getLegacyEnc(std::string_view input);
/// Convert from UTF-8 to sourceEncoding.
/// Returns a view to internal buffer invalidate by next getUtf8 or getLegacyEnc call if input is not
/// ASCII-only string. Otherwise returns a view to the input.
std::string_view getLegacyEnc(std::string_view input);
private:
std::string mBuffer;
StatelessUtf8Encoder mImpl;
private:
std::string mBuffer;
StatelessUtf8Encoder mImpl;
};
}