| 97 | } |
| 98 | |
| 99 | TextView |
| 100 | query_value_update(Context &ctx, TextView qs, TextView name, Feature const &value, bool case_p, bool force_equal_p) |
| 101 | { |
| 102 | TextView zret; |
| 103 | bool nv_is_nil_p = (value.value_type() == NIL); |
| 104 | if (qs.empty()) { |
| 105 | if (!nv_is_nil_p) { |
| 106 | zret = ctx.render_transient([&](BufferWriter &w) { w.print("{}={}", name, std::get<STRING>(value)); }); |
| 107 | } |
| 108 | } else { // invariant - query string was not empty. |
| 109 | auto &&[k, v] = ts::query_value_for(qs, name, case_p); |
| 110 | if (k.empty()) { // not found at all. |
| 111 | if (!nv_is_nil_p) { |
| 112 | zret = ctx.render_transient([&](BufferWriter &w) { w.print("{}&{}={}", qs, name, value); }); |
| 113 | } |
| 114 | } else { |
| 115 | // Make a note if there was no value but an '=' anyway. |
| 116 | bool equal_p = force_equal_p || (v.empty() && (v.data() != k.end())); |
| 117 | // Prefix is the part before the name. |
| 118 | TextView prefix{qs.data(), k.data()}; |
| 119 | // Suffix is the part after the value. |
| 120 | TextView suffix{v.end(), qs.end()}; |
| 121 | if (!nv_is_nil_p) { |
| 122 | zret = ctx.render_transient([&, k = k](BufferWriter &w) { |
| 123 | w.write(prefix).write(k); |
| 124 | if (equal_p || !(value.index() == IndexFor(STRING) && std::get<IndexFor(STRING)>(value).empty())) { |
| 125 | w.print("={}", value); |
| 126 | } |
| 127 | w.write(suffix); |
| 128 | }); |
| 129 | } else { // NIL, remove the pair. |
| 130 | prefix.rtrim("&;"_tv); |
| 131 | suffix.ltrim("&;"_tv); |
| 132 | if (suffix.empty()) { |
| 133 | zret = prefix; |
| 134 | } else if (prefix.empty()) { |
| 135 | zret = suffix; |
| 136 | } else { // neither is empty. |
| 137 | zret = ctx.render_transient([&](BufferWriter &w) { w.print("{}&{}", prefix, suffix); }); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | return zret; |
| 143 | } |
| 144 | |
| 145 | } // namespace |
| 146 | /* ------------------------------------------------------------------------------------ */ |
no test coverage detected