diff --git a/ReferenceTests/Retro68/CMakeLists.txt b/ReferenceTests/Retro68/CMakeLists.txt index 5aac916..f6ad228 100644 --- a/ReferenceTests/Retro68/CMakeLists.txt +++ b/ReferenceTests/Retro68/CMakeLists.txt @@ -3,3 +3,4 @@ project(GraphiteReferenceTests C CXX) add_application(EqualStringCapture EqualStringCapture.c) add_application(FileManagerCapture FileManagerCapture.c) +add_application(RelStringCapture RelStringCapture.c) diff --git a/ReferenceTests/Retro68/README.md b/ReferenceTests/Retro68/README.md index 2be1d9a..44b8d31 100644 --- a/ReferenceTests/Retro68/README.md +++ b/ReferenceTests/Retro68/README.md @@ -101,3 +101,26 @@ removes the folder. Its output on Mac OS 9.2.2 is then fails with wrPermErr (-61). Opening it for writing returned -5000 (afpAccessDenied) because File Sharing was on; without it the File Manager documents permErr (-54), which Graphite returns. + +## RelString + +`RelStringCapture.c` records RelString, the ordering HFS sorts catalog names +by, for every pair of one-byte strings in each combination of case and +diacritic sensitivity, then for a list of multi-byte cases. Its output on Mac +OS 9.2.2 is `RelString-macos-9.2.2-ppc.bin` (format `GRS1`, described in the +source). SHA-256: +`a7f76c55b4c4d23cd6542452957e47383412c2eeb20716a7b19caf3af314b475` + +- Each mode is a total order of the 256 bytes, and the bytes it finds equal + are exactly those EqualString finds equal. +- Strings compare in one pass, byte by byte, the first difference deciding: + with diacritics significant, "éx" sorts after "ez". A prefix sorts before + the longer string, an embedded NUL included. +- Case-sensitive, uppercase sorts before lowercase. Space and the + non-breaking space (0xCA) are equal in every mode. +- The diacritic-insensitive orders are not the fully sensitive order applied + to folded letters, so Graphite keeps one measured key table per mode + (`Sources/GraphiteCore/RelStringKeys.swift`). + +`TextUtilitiesTests/relStringMatchesMacOS9()` checks Graphite against the +capture, or against `GRAPHITE_RELSTRING_CAPTURE` when it is set. diff --git a/ReferenceTests/Retro68/RelString-macos-9.2.2-ppc.bin b/ReferenceTests/Retro68/RelString-macos-9.2.2-ppc.bin new file mode 100644 index 0000000..569fc0c Binary files /dev/null and b/ReferenceTests/Retro68/RelString-macos-9.2.2-ppc.bin differ diff --git a/ReferenceTests/Retro68/RelStringCapture.c b/ReferenceTests/Retro68/RelStringCapture.c new file mode 100644 index 0000000..789c112 --- /dev/null +++ b/ReferenceTests/Retro68/RelStringCapture.c @@ -0,0 +1,83 @@ +/* + * Captures RelString, the ordering HFS catalogs sort names by, into + * "RelString.bin" in the application's folder. It refuses to overwrite an + * existing capture. + * + * Format GRS1: four ASCII magic bytes; four 256 x 256 matrices of one-byte + * pairs in mode order caseSensitive * 2 + diacriticSensitive, rows in + * first-byte order, each byte RelString's result (-1, 0 or 1) as a signed + * byte; then the count of multi-byte cases and, for each, its four results + * in mode order. The cases are listed in `cases` below, in order, and in + * Graphite's TextUtilitiesTests. + */ +#include +#include + +static const unsigned char header[] = { 'G', 'R', 'S', '1' }; +static const unsigned char fileName[] = { 13, 'R', 'e', 'l', 'S', 't', 'r', 'i', 'n', 'g', '.', 'b', 'i', 'n' }; +static signed char row[256]; + +/* Pascal strings: a length byte, then the bytes. */ +static const unsigned char *const cases[][2] = { + { (const unsigned char *)"\001a", (const unsigned char *)"\001a" }, + { (const unsigned char *)"\001a", (const unsigned char *)"\002ab" }, + { (const unsigned char *)"\002ab", (const unsigned char *)"\001a" }, + { (const unsigned char *)"\000", (const unsigned char *)"\001a" }, + { (const unsigned char *)"\001a", (const unsigned char *)"\000" }, + { (const unsigned char *)"\000", (const unsigned char *)"\000" }, + { (const unsigned char *)"\002ab", (const unsigned char *)"\001b" }, + { (const unsigned char *)"\001b", (const unsigned char *)"\002ab" }, + { (const unsigned char *)"\002Ab", (const unsigned char *)"\002aB" }, + { (const unsigned char *)"\002aB", (const unsigned char *)"\002Ab" }, + { (const unsigned char *)"\003abc", (const unsigned char *)"\003abd" }, + { (const unsigned char *)"\003ABC", (const unsigned char *)"\003abd" }, + { (const unsigned char *)"\002e\216", (const unsigned char *)"\002ee" }, + { (const unsigned char *)"\002\216x", (const unsigned char *)"\002ez" }, + { (const unsigned char *)"\003a b", (const unsigned char *)"\003a\312b" }, + { (const unsigned char *)"\004Zeta", (const unsigned char *)"\005alpha" }, + { (const unsigned char *)"\006File10", (const unsigned char *)"\005File2" }, + { (const unsigned char *)"\002a\000", (const unsigned char *)"\001a" }, + { (const unsigned char *)"\002\216a", (const unsigned char *)"\002Eb" }, + { (const unsigned char *)"\002Ea", (const unsigned char *)"\002\216b" }, +}; + +static OSErr writeBytes(short ref, const void *bytes, long length) +{ + long actual = length; + OSErr error = FSWrite(ref, &actual, bytes); + return error != noErr ? error : (actual == length ? noErr : writErr); +} + +int main(void) +{ + unsigned char first[2] = { 1, 0 }, second[2] = { 1, 0 }; + unsigned char count = sizeof(cases) / sizeof(cases[0]); + signed char results[4]; + short ref; + int mode, a, b, i; + OSErr error; + + error = Create(fileName, 0, 'Grph', 'DATA'); + if (error != noErr) return 1; + error = FSOpen(fileName, 0, &ref); + if (error != noErr) return 2; + error = writeBytes(ref, header, sizeof(header)); + for (mode = 0; mode < 4 && error == noErr; ++mode) { + for (a = 0; a < 256 && error == noErr; ++a) { + first[1] = a; + for (b = 0; b < 256; ++b) { + second[1] = b; + row[b] = (signed char)RelString(first, second, (mode & 2) != 0, (mode & 1) != 0); + } + error = writeBytes(ref, row, sizeof(row)); + } + } + if (error == noErr) error = writeBytes(ref, &count, 1); + for (i = 0; i < count && error == noErr; ++i) { + for (mode = 0; mode < 4; ++mode) + results[mode] = (signed char)RelString(cases[i][0], cases[i][1], (mode & 2) != 0, (mode & 1) != 0); + error = writeBytes(ref, results, sizeof(results)); + } + if (FSClose(ref) != noErr) return 3; + return error == noErr ? 0 : 4; +} diff --git a/Sources/GraphiteCore/RelStringKeys.swift b/Sources/GraphiteCore/RelStringKeys.swift new file mode 100644 index 0000000..db8c865 --- /dev/null +++ b/Sources/GraphiteCore/RelStringKeys.swift @@ -0,0 +1,95 @@ +// RelString's ordering: for each combination of case and diacritic +// sensitivity, every byte's sort key. RelString compares two strings byte by +// byte through these keys; the first difference decides, and when one string +// is a prefix of the other the shorter sorts first. Bytes with equal keys are +// those EqualString finds equal. +// +// The keys are measured, not derived: they rank the 256 bytes as RelString +// on Mac OS 9.2.2 (PowerPC, under UTM) ordered every pair of one-byte strings, +// captured by ReferenceTests/Retro68/RelStringCapture.c into +// ReferenceTests/Retro68/RelString-macos-9.2.2-ppc.bin. TextUtilitiesTests +// checks all 262,144 pairs against that capture. + +extension TextUtilities { + + /// The sort keys, indexed by caseSensitive * 2 + diacriticSensitive, then + /// by byte. + static let relStringKeys: [[UInt8]] = [ + // caseInsensitiveDiacriticInsensitive + [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0x00 + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 0x10 + 32, 33, 34, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, // 0x20 + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, // 0x30 + 70, 71, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, // 0x40 + 89, 90, 91, 92, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, // 0x50 + 73, 71, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, // 0x60 + 89, 90, 91, 92, 94, 95, 96, 97, 98, 99, 100, 106, 107, 108, 109, 110, // 0x70 + 71, 71, 75, 77, 86, 87, 95, 71, 71, 71, 71, 71, 71, 75, 77, 77, // 0x80 + 77, 77, 81, 81, 81, 81, 86, 87, 87, 87, 87, 87, 95, 95, 95, 95, // 0x90 + 111, 112, 113, 114, 115, 116, 117, 93, 118, 119, 120, 121, 122, 123, 72, 87, // 0xA0 + 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 71, 87, 135, 72, 87, // 0xB0 + 136, 137, 138, 139, 140, 141, 142, 37, 38, 143, 32, 71, 71, 87, 88, 88, // 0xC0 + 144, 145, 35, 36, 44, 45, 146, 147, 99, 148, 149, 150, 151, 152, 153, 154, // 0xD0 + 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, // 0xE0 + 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, // 0xF0 + ], + // caseInsensitiveDiacriticSensitive + [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0x00 + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 0x10 + 32, 33, 34, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, // 0x20 + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, // 0x30 + 70, 71, 81, 82, 84, 85, 90, 91, 92, 93, 98, 99, 100, 101, 102, 104, // 0x40 + 113, 114, 115, 116, 118, 119, 124, 125, 126, 127, 129, 130, 131, 132, 133, 134, // 0x50 + 77, 71, 81, 82, 84, 85, 90, 91, 92, 93, 98, 99, 100, 101, 102, 104, // 0x60 + 113, 114, 115, 116, 118, 119, 124, 125, 126, 127, 129, 135, 136, 137, 138, 139, // 0x70 + 73, 75, 83, 86, 103, 105, 120, 78, 72, 79, 73, 74, 75, 83, 86, 87, // 0x80 + 88, 89, 94, 95, 96, 97, 103, 109, 110, 111, 105, 106, 121, 122, 123, 120, // 0x90 + 140, 141, 142, 143, 144, 145, 146, 117, 147, 148, 149, 150, 151, 152, 76, 107, // 0xA0 + 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 80, 112, 164, 76, 107, // 0xB0 + 165, 166, 167, 168, 169, 170, 171, 37, 38, 172, 32, 72, 74, 106, 108, 108, // 0xC0 + 173, 174, 35, 36, 44, 45, 175, 176, 128, 177, 178, 179, 180, 181, 182, 183, // 0xD0 + 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, // 0xE0 + 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, // 0xF0 + ], + // caseSensitiveDiacriticInsensitive + [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0x00 + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 0x10 + 32, 33, 34, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, // 0x20 + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, // 0x30 + 70, 71, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, // 0x40 + 105, 107, 109, 111, 114, 116, 118, 120, 122, 124, 126, 128, 129, 130, 131, 132, // 0x50 + 133, 73, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 103, // 0x60 + 106, 108, 110, 112, 115, 117, 119, 121, 123, 125, 127, 134, 135, 136, 137, 138, // 0x70 + 71, 71, 77, 81, 99, 101, 116, 73, 73, 73, 73, 73, 73, 78, 82, 82, // 0x80 + 82, 82, 90, 90, 90, 90, 100, 103, 103, 103, 103, 103, 117, 117, 117, 117, // 0x90 + 139, 140, 141, 142, 143, 144, 145, 113, 146, 147, 148, 149, 150, 151, 72, 101, // 0xA0 + 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 73, 103, 163, 74, 103, // 0xB0 + 164, 165, 166, 167, 168, 169, 170, 37, 38, 171, 32, 71, 71, 101, 102, 104, // 0xC0 + 172, 173, 35, 36, 44, 45, 174, 175, 125, 176, 177, 178, 179, 180, 181, 182, // 0xD0 + 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, // 0xE0 + 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, // 0xF0 + ], + // caseSensitiveDiacriticSensitive + [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0x00 + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 0x10 + 32, 33, 34, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, // 0x20 + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, // 0x30 + 70, 71, 86, 88, 92, 94, 101, 103, 105, 107, 113, 115, 117, 119, 121, 125, // 0x40 + 139, 141, 143, 145, 148, 150, 157, 159, 161, 163, 166, 168, 169, 170, 171, 172, // 0x50 + 173, 77, 87, 90, 93, 96, 102, 104, 106, 108, 114, 116, 118, 120, 123, 130, // 0x60 + 140, 142, 144, 146, 149, 152, 158, 160, 162, 164, 167, 174, 175, 176, 177, 178, // 0x70 + 73, 75, 89, 95, 122, 126, 151, 78, 79, 80, 81, 82, 83, 91, 97, 98, // 0x80 + 99, 100, 109, 110, 111, 112, 124, 131, 132, 133, 134, 135, 153, 154, 155, 156, // 0x90 + 179, 180, 181, 182, 183, 184, 185, 147, 186, 187, 188, 189, 190, 191, 76, 128, // 0xA0 + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 84, 137, 203, 85, 136, // 0xB0 + 204, 205, 206, 207, 208, 209, 210, 37, 38, 211, 32, 72, 74, 127, 129, 138, // 0xC0 + 212, 213, 35, 36, 44, 45, 214, 215, 165, 216, 217, 218, 219, 220, 221, 222, // 0xD0 + 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, // 0xE0 + 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, // 0xF0 + ], + ] +} diff --git a/Sources/GraphiteCore/TextUtilities.swift b/Sources/GraphiteCore/TextUtilities.swift index e9aafed..a1d4a32 100644 --- a/Sources/GraphiteCore/TextUtilities.swift +++ b/Sources/GraphiteCore/TextUtilities.swift @@ -18,6 +18,26 @@ public enum TextUtilities { } } + /// RelString: how the first string sorts against the second, -1 before, + /// 0 equal or 1 after, by the ordering HFS sorts catalog names with. + /// Strings compare byte by byte through `relStringKeys`, the first + /// difference deciding; a prefix sorts before the longer string. This + /// operation does not apply the rules of other script systems. + public static func relString( + _ first: PascalString, + _ second: PascalString, + caseSensitive: Bool, + diacriticSensitive: Bool + ) -> Int16 { + let keys = relStringKeys[(caseSensitive ? 2 : 0) + (diacriticSensitive ? 1 : 0)] + for (left, right) in zip(first.bytes, second.bytes) { + let (a, b) = (keys[Int(left)], keys[Int(right)]) + if a != b { return a < b ? -1 : 1 } + } + if first.count == second.count { return 0 } + return first.count < second.count ? -1 : 1 + } + // The original Macintosh character set stops at 0xD8. Executor's // osutil.c records comparison tables measured on a Macintosh: // https://github.com/ctm/executor/blob/master/src/osutil.c diff --git a/Tests/GraphiteCoreTests/TextUtilitiesTests.swift b/Tests/GraphiteCoreTests/TextUtilitiesTests.swift index ff585f0..63566f2 100644 --- a/Tests/GraphiteCoreTests/TextUtilitiesTests.swift +++ b/Tests/GraphiteCoreTests/TextUtilitiesTests.swift @@ -110,4 +110,80 @@ import Foundation } } } + + // MARK: RelString + + /// The multi-byte cases of RelStringCapture.c, in its order. + private static let relStringCases: [([UInt8], [UInt8])] = [ + (Array("a".utf8), Array("a".utf8)), + (Array("a".utf8), Array("ab".utf8)), + (Array("ab".utf8), Array("a".utf8)), + ([], Array("a".utf8)), + (Array("a".utf8), []), + ([], []), + (Array("ab".utf8), Array("b".utf8)), + (Array("b".utf8), Array("ab".utf8)), + (Array("Ab".utf8), Array("aB".utf8)), + (Array("aB".utf8), Array("Ab".utf8)), + (Array("abc".utf8), Array("abd".utf8)), + (Array("ABC".utf8), Array("abd".utf8)), + ([0x65, 0x8E], Array("ee".utf8)), + ([0x8E, 0x78], Array("ez".utf8)), + ([0x61, 0x20, 0x62], [0x61, 0xCA, 0x62]), + (Array("Zeta".utf8), Array("alpha".utf8)), + (Array("File10".utf8), Array("File2".utf8)), + ([0x61, 0x00], Array("a".utf8)), + ([0x8E, 0x61], Array("Eb".utf8)), + (Array("Ea".utf8), [0x8E, 0x62]), + ] + + @Test func relStringMatchesMacOS9() throws { + let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent() + .deletingLastPathComponent().deletingLastPathComponent() + let fixture = root.appendingPathComponent("ReferenceTests/Retro68/RelString-macos-9.2.2-ppc.bin") + let path = ProcessInfo.processInfo.environment["GRAPHITE_RELSTRING_CAPTURE"] + let bytes = [UInt8](try Data(contentsOf: path.map { URL(fileURLWithPath: $0) } ?? fixture)) + let matrices = 4 + 4 * 256 * 256 + guard bytes.count == matrices + 1 + Self.relStringCases.count * 4, + Array(bytes.prefix(4)) == Array("GRS1".utf8), + Int(bytes[matrices]) == Self.relStringCases.count else { + Issue.record("Invalid or incomplete RelString capture") + return + } + var mismatches = 0 + for mode in 0 ..< 4 { + for first in 0 ..< 256 { + for second in 0 ..< 256 { + let expected = Int16(Int8(bitPattern: bytes[4 + (mode * 256 + first) * 256 + second])) + let actual = TextUtilities.relString(PascalString(bytes: [UInt8(first)]), PascalString(bytes: [UInt8(second)]), + caseSensitive: mode & 2 != 0, diacriticSensitive: mode & 1 != 0) + if actual != expected { + mismatches += 1 + if mismatches <= 10 { Issue.record("RelString mismatch: mode \(mode), bytes \(first), \(second)") } + } + } + } + } + #expect(mismatches == 0) + for (index, (first, second)) in Self.relStringCases.enumerated() { + for mode in 0 ..< 4 { + let expected = Int16(Int8(bitPattern: bytes[matrices + 1 + index * 4 + mode])) + let actual = TextUtilities.relString(PascalString(bytes: first), PascalString(bytes: second), + caseSensitive: mode & 2 != 0, diacriticSensitive: mode & 1 != 0) + #expect(actual == expected, "case \(index + 1), mode \(mode)") + } + } + } + + @Test func relStringOrdersHFSNames() { + func order(_ a: PascalString, _ b: PascalString) -> Int16 { + TextUtilities.relString(a, b, caseSensitive: false, diacriticSensitive: true) + } + // HFS's comparison: case does not matter, diacritics do, and a + // prefix sorts first. + #expect(order("readme", "ReadMe") == 0) + #expect(order("Read", "Read Me") == -1) + #expect(order("Zeta", "alpha") == 1) + #expect(order(PascalString(bytes: [0x8E]), "e") == 1) + } }