mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-29 00:03:17 +00:00
Add string decode/encode api
This commit is contained in:
@@ -95,6 +95,8 @@ from .sql import sqlite, sqlite_entity
|
||||
|
||||
rocksdb_lazy_instance = file_module.rocksdb_lazy_instance
|
||||
|
||||
decode_spf_string = ifcopenshell_wrapper.decode_spf_string
|
||||
encode_spf_string = ifcopenshell_wrapper.encode_spf_string
|
||||
get_log = ifcopenshell_wrapper.get_log
|
||||
logger = ifcopenshell_wrapper.logger if hasattr(ifcopenshell_wrapper, "logger") else None
|
||||
if hasattr(ifcopenshell_wrapper, "logger_or_root"):
|
||||
@@ -114,6 +116,8 @@ def optional_logger_args(logger: ifcopenshell_wrapper.logger | None) -> tuple[lo
|
||||
# (it's a requirement for a typed library)
|
||||
__all__ = [
|
||||
"clear_plugin_search_paths",
|
||||
"decode_spf_string",
|
||||
"encode_spf_string",
|
||||
"entity_instance",
|
||||
"file",
|
||||
"get_plugin_search_paths",
|
||||
|
||||
@@ -1649,6 +1649,8 @@ def construct_iterator_with_include_exclude_id(
|
||||
geometry_library, settings, file, elems, include, num_threads, logger=None
|
||||
): ...
|
||||
def convert_loop_to_function_item(loop): ...
|
||||
def decode_spf_string(value: str) -> str: ...
|
||||
def encode_spf_string(value: str) -> str: ...
|
||||
|
||||
class attribute_value_derived: ...
|
||||
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import ifcopenshell
|
||||
|
||||
|
||||
def test_spf_strings_can_be_encoded_and_decoded():
|
||||
decoded = "Café's \\"
|
||||
encoded = r"'Caf\X2\00E9\X0\''s \\'"
|
||||
|
||||
assert ifcopenshell.encode_spf_string(decoded) == encoded
|
||||
assert ifcopenshell.decode_spf_string(encoded) == decoded
|
||||
|
||||
|
||||
def test_skip_over_non_entity_instance():
|
||||
data = """
|
||||
ISO-10303-21;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -962,6 +962,12 @@ private:
|
||||
};
|
||||
|
||||
%include "../ifcparse/ifc_parse_api.h"
|
||||
|
||||
namespace ifcopenshell {
|
||||
std::string encode_spf_string(const std::string& value);
|
||||
std::string decode_spf_string(const std::string& value);
|
||||
}
|
||||
|
||||
%include "../ifcparse/spf_header.h"
|
||||
|
||||
%pythoncode %{
|
||||
|
||||
Reference in New Issue
Block a user