OpenTTD Source  14.0-RC1
strgen_base.cpp File Reference
#include "../stdafx.h"
#include "../core/alloc_func.hpp"
#include "../core/endian_func.hpp"
#include "../core/mem_func.hpp"
#include "../error_func.h"
#include "../string_func.h"
#include "../table/control_codes.h"
#include "strgen.h"
#include <charconv>
#include "../table/strgen_tables.h"
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

struct  Buffer
 The buffer for writing a single string. More...
 

Functions

static const CmdStruct * ParseCommandString (const char **str, char *param, int *argno, int *casei)
 
size_t Utf8Validate (const char *s)
 
void EmitSingleChar (Buffer *buffer, char *buf, int value)
 
bool ParseRelNum (char **buf, int *value, int *offset)
 
char * ParseWord (char **buf)
 
static int TranslateArgumentIdx (int arg, int offset=0)
 
static void EmitWordList (Buffer *buffer, const std::vector< const char * > &words, uint nw)
 
void EmitPlural (Buffer *buffer, char *buf, int)
 
void EmitGender (Buffer *buffer, char *buf, int)
 
static const CmdStruct * FindCmd (const char *s, int len)
 
static uint ResolveCaseName (const char *str, size_t len)
 
ParsedCommandStruct ExtractCommandString (const char *s, bool)
 
const CmdStruct * TranslateCmdForCompare (const CmdStruct *a)
 
static bool CheckCommandsMatch (const char *a, const char *b, const char *name)
 
static void StripTrailingWhitespace (std::string &str)
 
static void PutArgidxCommand (Buffer *buffer)
 
static void PutCommandString (Buffer *buffer, const char *str)
 
static std::string ReplaceNBSP (std::string string)
 
std::optional< std::string > ParseNumberFormatSeparators (NumberFormatSeparators &separators, std::string_view format, size_t length)
 Parse the NumberFormatSeparators out of the given format string, with the expected number of digits. More...
 
std::optional< std::string > ParseNumberAbbreviations (NumberAbbreviations &abbreviations, std::string_view input)
 Parse the NumberAbbreviations out of the given input string. More...
 

Variables

static bool _translated
 Whether the current language is not the master language.
 
static bool _translation
 Is the current file actually a translation or not.
 
const char * _file = "(unknown file)"
 The filename of the input, so we can refer to it in errors/warnings.
 
int _cur_line
 The current line we're parsing in the input file.
 
int _errors
 
int _warnings
 
int _show_todo
 
LanguagePackHeader _lang
 Header information about a language.
 
static const ptrdiff_t MAX_COMMAND_PARAM_SIZE = 100
 Maximum size of every command block, not counting the name of the command itself.
 
static const char * _cur_ident
 
static ParsedCommandStruct _cur_pcs
 
static int _cur_argidx
 
static const std::string_view NBSP_TOKEN = "{NBSP}"
 

Detailed Description

Tool to create computer readable (stand-alone) translation files.

Definition in file strgen_base.cpp.

Function Documentation

◆ ParseNumberAbbreviations()

std::optional<std::string> ParseNumberAbbreviations ( NumberAbbreviations &  abbreviations,
std::string_view  input 
)

Parse the NumberAbbreviations out of the given input string.

In some places in the UI numbers are getting really big yet their exact value is not that important. For example in the graphs of company values. For this you want more compact number, e.g. 123 m for 123.456.789. However, due to the grouping of digits differing in different cultures, see ParseNumberFormatSeparators, there are many different ways of grouping digits.

This function builds up a lookup table of these abbreviations by power of ten. The input will be a list of definitions per power separator by a pipe character (|). Each definition is the power of ten and and the associated number format with DIGITS_IN_UINT64_T - power digits, separated by the equals sign (=).

For example, for English it defines every third power of ten with subsequently smaller number formats: 3=00,000,000,000,000,000{NBSP}k|6=00,000,000,000,000{NBSP}m|9=00,000,000,000{NBSP}bn|12=00,000,000{NBSP}tn|15=00,000{NBSP}Qa|18=00{NBSP}Qi

Parameters
abbreviationsThe table to write the abbreviations in; is will be cleared before filling.
inputThe input format to parse.
Returns
An std::optional with the error message, or std::nullopt when the parsing went without problems.

Definition at line 1063 of file strgen_base.cpp.

References DIGITS_IN_UINT64_T.

◆ ParseNumberFormatSeparators()

std::optional<std::string> ParseNumberFormatSeparators ( NumberFormatSeparators &  separators,
std::string_view  format,
size_t  length 
)

Parse the NumberFormatSeparators out of the given format string, with the expected number of digits.

Different cultures have different ways to separate their numbers when they get really big. In the Western world these are often called thousands separators which come every three digits counted from the back. The actual separator differs per language/country. In Chinese, Japanese and Korean they add a character every four digits counted from the back, and this character differs for each spot as it denotes "ten thousand", "hundred million", etc. In the Indic numbering system (Indian subcontinent), the first separator is after three digits counted from the back, but the next separators are given every two digits.

So, there's no simple single parameter that you can add to the digit grouping character that is already configured. The simplest solution is just defining what character to place between each of the digits, i.e what characters separate each of the digits. These are the NumberFormatSeparators.

To define these, you simply write a string of length zeros and then add any characters in between at the right locations so the digit grouping is correct. When formatting numbers, it will start at the appropriate digit and continue from there with separators.

Examples of formats are "00,000,000,000,000,000,000" and "0000{NBSP}0000{NBSP}0000{NBSP}0000{NBSP}0000".

Parameters
separatorsThe separators to fill; it will be cleared first.
formatThe format that is going to be read.
lengthThe number of digits that are expected in this format.
Returns
An std::optional with the error message, or std::nullopt when the parsing went without problems.

Definition at line 1022 of file strgen_base.cpp.