* Replace a parameter of this EncodedString. * @note If the string cannot be decoded for some reason, an empty EncodedString will be returned instead. * @param param Index of parameter to replace. * @param data New data for parameter. * @returns a new EncodedString with the parameter replaced. */
| 148 | * @returns a new EncodedString with the parameter replaced. |
| 149 | */ |
| 150 | EncodedString EncodedString::ReplaceParam(size_t param, StringParameter &&data) const |
| 151 | { |
| 152 | if (this->empty()) return {}; |
| 153 | |
| 154 | std::vector<StringParameter> params; |
| 155 | StringConsumer consumer(this->string); |
| 156 | |
| 157 | if (!consumer.ReadUtf8If(SCC_ENCODED_INTERNAL)) return {}; |
| 158 | |
| 159 | StringID str; |
| 160 | if (auto r = consumer.TryReadIntegerBase<uint32_t>(16); r.has_value()) { |
| 161 | str = *r; |
| 162 | } else { |
| 163 | return {}; |
| 164 | } |
| 165 | if (consumer.AnyBytesLeft() && !consumer.ReadUtf8If(SCC_RECORD_SEPARATOR)) return {}; |
| 166 | |
| 167 | while (consumer.AnyBytesLeft()) { |
| 168 | StringConsumer record(consumer.ReadUntilUtf8(SCC_RECORD_SEPARATOR, StringConsumer::SKIP_ONE_SEPARATOR)); |
| 169 | |
| 170 | if (!record.AnyBytesLeft()) { |
| 171 | /* This is an empty parameter. */ |
| 172 | params.emplace_back(std::monostate{}); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | /* Get the parameter type. */ |
| 177 | char32_t parameter_type = record.ReadUtf8(); |
| 178 | switch (parameter_type) { |
| 179 | case SCC_ENCODED_NUMERIC: { |
| 180 | uint64_t value = record.ReadIntegerBase<uint64_t>(16); |
| 181 | assert(!record.AnyBytesLeft()); |
| 182 | params.emplace_back(value); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | case SCC_ENCODED_STRING: { |
| 187 | params.emplace_back(record.Read(StringConsumer::npos)); |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | default: |
| 192 | /* Unknown parameter, make it blank. */ |
| 193 | params.emplace_back(std::monostate{}); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (param >= std::size(params)) return {}; |
| 199 | params[param] = data; |
| 200 | return GetEncodedStringWithArgs(str, params); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Decode the encoded string. |
no test coverage detected