#include "xpath/internal/xpath_eval.hpp" #include "xpath/internal/xpath_functions.hpp" #include "utils/utf8.hpp" #include "utils/xml_tokens.hpp" #include namespace beresta::xpath::detail { // substring-after(строка, разделитель): часть после первого вхождения // substring-after(string, separator): the part after the first occurrence XPathValue functionSubstringAfter(const FunctionCall& call) { std::string text = stringArgument(call, 0); std::string separator = stringArgument(call, 1); std::size_t found = text.find(separator); if (found == std::string::npos) return XPathValue::fromString(""); return XPathValue::fromString(text.substr(found + separator.size())); } /* ** substring(строка, начало, длина?) — позиции символов с единицы. ** Берутся символы p с round(начало) <= p < round(начало) + round(длина); ** сравнения с NaN ложны, поэтому крайние случаи работают по спецификации. ** ** substring(string, start, length?) — character positions from one. ** The characters p with round(start) <= p < round(start) + round(length) are taken; ** comparisons with NaN are false, so the edge cases work by the specification. */ XPathValue functionSubstring(const FunctionCall& call) { std::vector codes = toCodePoints(stringArgument(call, 0)); double start = std::floor(numberArgument(call, 1) + 0.5); double end = HUGE_VAL; std::size_t first = codes.size(); std::size_t last = 0; if (call.arguments.size() > 2) end = start + std::floor(numberArgument(call, 2) + 0.5); for (std::size_t index = 0; index < codes.size(); ++index) { double position = static_cast(index + 1); if (position >= start && position < end) { if (first == codes.size()) first = index; last = index + 1; } } return XPathValue::fromString(fromCodePoints(codes, first, last)); } // string-length(строка?): длина в символах, а не в байтах // string-length(string?): the length in characters, not in bytes XPathValue functionStringLength(const FunctionCall& call) { return XPathValue::fromNumber( static_cast(utils::countCodePoints(stringArgument(call, 0)))); } // normalize-space(строка?): пробелы по краям убираются, внутри — сжимаются до одного // normalize-space(string?): spaces at the edges are removed, inside they are collapsed to one XPathValue functionNormalizeSpace(const FunctionCall& call) { std::string text = stringArgument(call, 0); std::string result; for (std::string_view token : utils::splitTokens(text)) { if (!result.empty()) result += ' '; result.append(token); } return XPathValue::fromString(result); } /* ** translate(строка, откуда, куда): символ из «откуда» заменяется ** символом с той же позицией в «куда» или удаляется, если там его нет. ** ** translate(string, from, to): a character from "from" is replaced with ** the character at the same position in "to" or removed if there is none there. */ XPathValue functionTranslate(const FunctionCall& call) { std::vector text = toCodePoints(stringArgument(call, 0)); std::vector from = toCodePoints(stringArgument(call, 1)); std::vector to = toCodePoints(stringArgument(call, 2)); std::string result; for (std::uint32_t code : text) { std::size_t position = 0; while (position < from.size() && from[position] != code) ++position; if (position == from.size()) utils::appendUtf8(result, code); else if (position < to.size()) utils::appendUtf8(result, to[position]); } return XPathValue::fromString(result); } }