// K2321-K2325: editor.buffer — Editör UTF8 metin buffer // Invariant: Cursor byte değil geçerli metin sınırında durur // Positive: Türkçe ekle sil hareket // Counterexample: Çok baytın ortasında sil // Acceptance: Metin geçerli UTF8 kalır pub const EDITOR_BUFFER_SCHEMA: &str = "turkos.os.editor.buffer.v1"; pub const EDITOR_BUFFER_INVARIANT: &str = "Cursor byte değil geçerli metin sınırında durur"; pub const EDITOR_BUFFER_POSITIVE: &str = "Türkçe ekle sil hareket"; pub const EDITOR_BUFFER_COUNTEREXAMPLE: &str = "Çok baytın ortasında sil"; pub const EDITOR_BUFFER_ACCEPTANCE: &str = "Metin geçerli UTF8 kalır"; pub const EXPECTED_UTF8_SCALAR_EDITS: u32 = 6; pub const EXPECTED_CURSOR_GRAPHEME_STOPS: u32 = 8; pub const EXPECTED_MID_CODEPOINT_REFUSALS: u32 = 2; pub const EXPECTED_INVALID_UTF8_SEQUENCES: u32 = 0; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum EditorBufferError { DeleteInsideMultibyteCodepoint = 1, CursorAtRawByteBoundary = 2, BufferInvalidUtf8AfterEdit = 3, Utf8ScalarEditsMismatch = 4, CursorGraphemeStopsMismatch = 5, MidCodepointRefusalsMismatch = 6, ZeroBufferEpoch = 7, NonMonotonicBufferEpoch = 8, ZeroTextDigest = 9, UnboundEditorBufferDigest = 10, AlreadyCommitted = 11, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct EditorBufferRequest { pub delete_inside_multibyte_codepoint: bool, pub cursor_at_raw_byte_boundary: bool, pub text_remains_valid_utf8: bool, pub utf8_scalar_edits: u32, pub cursor_grapheme_stops: u32, pub mid_codepoint_refusals: u32, pub buffer_epoch: u64, pub previous_buffer_epoch: u64, pub text_digest: u64, pub buffer_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct EditorBufferReceipt { pub utf8_scalar_edits: u32, pub cursor_grapheme_stops: u32, pub mid_codepoint_refusals: u32, pub invalid_utf8_sequences: u32, pub text_remains_valid_utf8: bool, pub buffer_epoch: u64, pub text_digest: u64, pub receipt_digest: u64, } pub const fn compute_editor_buffer_binding_digest( utf8_scalar_edits: u32, cursor_grapheme_stops: u32, mid_codepoint_refusals: u32, invalid_utf8_sequences: u32, buffer_epoch: u64, text_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2325_4544_4255_4631).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (utf8_scalar_edits as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (cursor_grapheme_stops as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (mid_codepoint_refusals as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (invalid_utf8_sequences as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ buffer_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ text_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct EditorBufferCoordinator { committed: bool, committed_total: u64, rejected_total: u64, utf8_boundary_refusals: u64, buffer_accounting_refusals: u64, committed_receipt: Option, } impl EditorBufferCoordinator { pub const fn new() -> Self { Self { committed: false, committed_total: 0, rejected_total: 0, utf8_boundary_refusals: 0, buffer_accounting_refusals: 0, committed_receipt: None, } } pub fn is_committed(&self) -> bool { self.committed } pub fn committed_total(&self) -> u64 { self.committed_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn utf8_boundary_refusals(&self) -> u64 { self.utf8_boundary_refusals } pub fn buffer_accounting_refusals(&self) -> u64 { self.buffer_accounting_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_buffer_edit( &mut self, req: &EditorBufferRequest, ) -> Result { if self.committed { self.rejected_total += 1; return Err(EditorBufferError::AlreadyCommitted); } if req.delete_inside_multibyte_codepoint { self.rejected_total += 1; self.utf8_boundary_refusals += 1; return Err(EditorBufferError::DeleteInsideMultibyteCodepoint); } if req.cursor_at_raw_byte_boundary { self.rejected_total += 1; self.utf8_boundary_refusals += 1; return Err(EditorBufferError::CursorAtRawByteBoundary); } if !req.text_remains_valid_utf8 { self.rejected_total += 1; self.utf8_boundary_refusals += 1; return Err(EditorBufferError::BufferInvalidUtf8AfterEdit); } if req.utf8_scalar_edits != EXPECTED_UTF8_SCALAR_EDITS { self.rejected_total += 1; self.buffer_accounting_refusals += 1; return Err(EditorBufferError::Utf8ScalarEditsMismatch); } if req.cursor_grapheme_stops != EXPECTED_CURSOR_GRAPHEME_STOPS { self.rejected_total += 1; self.buffer_accounting_refusals += 1; return Err(EditorBufferError::CursorGraphemeStopsMismatch); } if req.mid_codepoint_refusals != EXPECTED_MID_CODEPOINT_REFUSALS { self.rejected_total += 1; self.buffer_accounting_refusals += 1; return Err(EditorBufferError::MidCodepointRefusalsMismatch); } if req.buffer_epoch == 0 { self.rejected_total += 1; return Err(EditorBufferError::ZeroBufferEpoch); } if req.buffer_epoch <= req.previous_buffer_epoch { self.rejected_total += 1; return Err(EditorBufferError::NonMonotonicBufferEpoch); } if req.text_digest == 0 { self.rejected_total += 1; return Err(EditorBufferError::ZeroTextDigest); } let expected_binding = compute_editor_buffer_binding_digest( req.utf8_scalar_edits, req.cursor_grapheme_stops, req.mid_codepoint_refusals, EXPECTED_INVALID_UTF8_SEQUENCES, req.buffer_epoch, req.text_digest, ); if req.buffer_binding_digest != expected_binding { self.rejected_total += 1; self.buffer_accounting_refusals += 1; return Err(EditorBufferError::UnboundEditorBufferDigest); } self.committed = true; self.committed_total += 1; let receipt = EditorBufferReceipt { utf8_scalar_edits: req.utf8_scalar_edits, cursor_grapheme_stops: req.cursor_grapheme_stops, mid_codepoint_refusals: req.mid_codepoint_refusals, invalid_utf8_sequences: EXPECTED_INVALID_UTF8_SEQUENCES, text_remains_valid_utf8: true, buffer_epoch: req.buffer_epoch, text_digest: req.text_digest, receipt_digest: expected_binding ^ 0x2325_ACE0_4255_4631, }; self.committed_receipt = Some(receipt); Ok(receipt) } }