diff --git a/src/export/exportIfc.test.ts b/src/export/exportIfc.test.ts index 03db52b9..801924f0 100644 --- a/src/export/exportIfc.test.ts +++ b/src/export/exportIfc.test.ts @@ -12,7 +12,7 @@ // Fixture-Muster gespiegelt von exportSchedule.test.ts. import { describe, it, expect } from "vitest"; -import { exportIfcSpf, ifcGuid } from "./exportIfc"; +import { exportIfcSpf, ifcGuid, ifcString } from "./exportIfc"; import type { Ceiling, ExtrudedSolid, @@ -371,3 +371,20 @@ describe("exportIfcSpf — IFC4-Export", () => { expect([...referenced].filter((id) => !defined.has(id))).toEqual([]); }); }); + +describe("STEP-Zeichenketten (ISO 10303-21)", () => { + it("Umlaute als \\X2\\…\\X0\\, ASCII unverändert, Apostroph verdoppelt", () => { + expect(ifcString("Gebäude")).toBe("'Geb\\X2\\00E4\\X0\\ude'"); + expect(ifcString("Grösse Ü")).toBe("'Gr\\X2\\00F6\\X0\\sse \\X2\\00DC\\X0\\'"); + expect(ifcString("it's a\\b")).toBe("'it''s a\\\\b'"); + }); + it("aufeinanderfolgende Zeichen in einem Block, Zeichen ausserhalb BMP als \\X4\\", () => { + expect(ifcString("äö")).toBe("'\\X2\\00E400F6\\X0\\'"); + expect(ifcString("a😀")).toBe("'a\\X4\\0001F600\\X0\\'"); + }); + it("Steuerzeichen werden kodiert, die Datei bleibt reines ASCII", () => { + expect(ifcString("a\nb")).toBe("'a\\X2\\000A\\X0\\b'"); + const spf = exportIfcSpf({ ...fixtureProject(), name: "Wohnüberbauung «Süd»" }); + expect(/^[\x20-\x7e\n\r]*$/.test(spf)).toBe(true); + }); +}); diff --git a/src/export/exportIfc.ts b/src/export/exportIfc.ts index 5ea5b436..ac29d237 100644 --- a/src/export/exportIfc.ts +++ b/src/export/exportIfc.ts @@ -125,12 +125,38 @@ export function ifcGuid(id: string): string { // ── STEP-Formatierung ─────────────────────────────────────────────────────── -/** STEP-String-Literal ('…', Apostroph verdoppelt, Backslash verdoppelt). */ -function S(s: string): string { - const escaped = s.replace(/\\/g, "\\\\").replace(/'/g, "''"); - return `'${escaped}'`; +/** + * STEP-String-Literal (ISO 10303-21): druckbares ASCII bleibt, Backslash und Apostroph werden + * verdoppelt, alles andere (Umlaute, Steuerzeichen) als \X2\HHHH…\X0\ (BMP) bzw. + * \X4\HHHHHHHH…\X0\ (ausserhalb der BMP). Die Datei bleibt so reines ASCII. + */ +export function ifcString(s: string): string { + let out = ""; + let run: number[] = []; + const flush = () => { + if (run.length === 0) return; + const wide = run.some((c) => c > 0xffff); + const hex = run.map((c) => c.toString(16).toUpperCase().padStart(wide ? 8 : 4, "0")).join(""); + out += `${wide ? "\\X4\\" : "\\X2\\"}${hex}\\X0\\`; + run = []; + }; + for (const ch of s) { + const c = ch.codePointAt(0)!; + if (c >= 0x20 && c <= 0x7e) { + flush(); + out += ch === "\\" ? "\\\\" : ch === "'" ? "''" : ch; + } else { + // \X2\ und \X4\ nicht mischen: Wechsel der Breite beginnt einen neuen Block. + if (run.length > 0 && (run[0] > 0xffff) !== (c > 0xffff)) flush(); + run.push(c); + } + } + flush(); + return `'${out}'`; } +const S = ifcString; + /** STEP-REAL-Literal — immer mit Dezimalpunkt, ohne unnötige Nachkommastellen. */ function R(x: number): string { const v = Object.is(x, -0) ? 0 : x;