MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GetEncodedStringWithArgs

Function GetEncodedStringWithArgs

src/strings.cpp:102–141  ·  view source on GitHub ↗

* Encode a string with its parameters into an encoded string. * The encoded string can be stored and decoded later without requiring parameters to be stored separately. * @param str The StringID to format. * @param params The parameters of the string. * @returns The encoded string. */

Source from the content-addressed store, hash-verified

100 * @returns The encoded string.
101 */
102EncodedString GetEncodedStringWithArgs(StringID str, std::span<const StringParameter> params)
103{
104 std::string result;
105 StringBuilder builder(result);
106 builder.PutUtf8(SCC_ENCODED_INTERNAL);
107 builder.PutIntegerBase(str, 16);
108
109 struct visitor {
110 StringBuilder &builder;
111
112 void operator()(const std::monostate &) {}
113
114 void operator()(const uint64_t &arg)
115 {
116 this->builder.PutUtf8(SCC_ENCODED_NUMERIC);
117 this->builder.PutIntegerBase(arg, 16);
118 }
119
120 void operator()(const std::string &value)
121 {
122#ifdef WITH_ASSERT
123 /* Don't allow an encoded string to contain another encoded string. */
124 {
125 auto [len, c] = DecodeUtf8(value);
126 assert(len == 0 || (c != SCC_ENCODED && c != SCC_ENCODED_INTERNAL && c != SCC_RECORD_SEPARATOR));
127 }
128#endif /* WITH_ASSERT */
129 this->builder.PutUtf8(SCC_ENCODED_STRING);
130 this->builder += value;
131 }
132 };
133
134 visitor v{builder};
135 for (const auto &param : params) {
136 builder.PutUtf8(SCC_RECORD_SEPARATOR);
137 std::visit(v, param.data);
138 }
139
140 return EncodedString{std::move(result)};
141}
142
143/**
144 * Replace a parameter of this EncodedString.

Callers 7

ShowNewGRFErrorFunction · 0.85
GetEncodedStringFunction · 0.85
ReplaceParamMethod · 0.85
GetEncodedStringFunction · 0.85
CheckOwnershipFunction · 0.85
CmdConvertRoadFunction · 0.85

Calls 2

PutUtf8Method · 0.80
PutIntegerBaseMethod · 0.80

Tested by

no test coverage detected