Add string decode/encode api

This commit is contained in:
Thomas Krijnen
2026-08-22 13:12:36 +02:00
parent a5e94cf0d8
commit 87bc6bfbab
8 changed files with 55 additions and 5 deletions
+1 -5
View File
@@ -67,10 +67,6 @@ class IFC_PARSE_API character_decoder {
std::string get(size_t& offset);
};
} // namespace ifcopenshell
namespace ifcopenshell {
class IFC_PARSE_API character_encoder {
private:
std::u32string str_;
@@ -80,6 +76,6 @@ class IFC_PARSE_API character_encoder {
operator std::string();
};
} // namespace IfcWrite
} // namespace ifcopenshell
#endif
+20
View File
@@ -550,6 +550,26 @@ std::string token::to_string() {
return result;
}
std::string ifcopenshell::encode_spf_string(const std::string& value) {
return character_encoder(value);
}
std::string ifcopenshell::decode_spf_string(const std::string& value) {
std::string wrapped;
auto value_p = &value;
if (!value.empty() && value.front() != '\'') {
wrapped = "'" + value + "'";
value_p = &wrapped;
}
file_reader<full_buffer_impl> reader(*value_p, caller_fed_tag{});
spf_lexer<file_reader<full_buffer_impl>> lexer(&reader);
token decoded = lexer.next();
if (!decoded.is_string()) {
throw exception("Expected an SPF string");
}
return decoded.as_string();
}
namespace {
template<typename Variant, typename T>
+4
View File
@@ -47,6 +47,10 @@ extern IFC_PARSE_API const char *IFCOPENSHELL_VERSION;
namespace ifcopenshell {
IFC_PARSE_API std::string encode_spf_string(const std::string& value);
IFC_PARSE_API std::string decode_spf_string(const std::string& value);
/// A stream of tokens to be read from a file_reader.
template <typename Reader>
class IFC_PARSE_API spf_lexer {
@@ -2,9 +2,19 @@
#include <catch2/catch_test_macros.hpp>
#include <ifcparse/file.h>
#include <ifcparse/parse.h>
#include <string>
#include <vector>
TEST_CASE("SPF strings can be encoded and decoded", "[ifcparse]") {
const std::string decoded = "Caf\xC3\xA9" "'s \\";
const std::string encoded = R"('Caf\X2\00E9\X0\''s \\')";
CHECK(ifcopenshell::encode_spf_string(decoded) == encoded);
CHECK(ifcopenshell::decode_spf_string(encoded) == decoded);
CHECK(ifcopenshell::decode_spf_string(encoded.substr(1, encoded.size() - 2)) == decoded);
}
TEST_CASE("IfcPropertySetDefinitionSet references are resolved without replacing their owner", "[ifcparse]") {
const std::string fixture = std::string(IFCOPENSHELL_TEST_FIXTURES) + "/ColumnPSetsOfSets.ifc";
ifcopenshell::file file(fixture);