interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / research / atemporal / at1 / evaluator / src / text.rs

150 lines · 5603 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Byte rules (AT1_CASE_V1 section 1) and the small token grammars shared by
17//! case and result files.
18
19/// Splits a file into lines after enforcing the byte rules. Err names the rule.
20pub fn lines_checked(bytes: &[u8]) -> Result<Vec<String>, String> {
21 if bytes.is_empty() {
22 return Err("empty file".into());
23 }
24 for (i, &b) in bytes.iter().enumerate() {
25 if b == b'\n' {
26 continue;
27 }
28 if !(0x20..=0x7e).contains(&b) {
29 return Err(format!("byte 0x{:02x} at offset {} not allowed", b, i));
30 }
31 }
32 if *bytes.last().unwrap() != b'\n' {
33 return Err("last line not LF-terminated".into());
34 }
35 let body = &bytes[..bytes.len() - 1];
36 let mut out = Vec::new();
37 for (n, raw) in body.split(|&b| b == b'\n').enumerate() {
38 let s = String::from_utf8(raw.to_vec()).map_err(|_| "not ASCII".to_string())?;
39 if s.is_empty() {
40 return Err(format!("blank line {}", n + 1));
41 }
42 if s.starts_with(' ') || s.ends_with(' ') {
43 return Err(format!("leading or trailing space on line {}", n + 1));
44 }
45 if s.contains(" ") {
46 return Err(format!("double space on line {}", n + 1));
47 }
48 out.push(s);
49 }
50 Ok(out)
51}
52
53pub fn join_lf(lines: &[String]) -> Vec<u8> {
54 let mut v = Vec::new();
55 for l in lines {
56 v.extend_from_slice(l.as_bytes());
57 v.push(b'\n');
58 }
59 v
60}
61
62pub fn is_label(s: &str) -> bool {
63 let b = s.as_bytes();
64 !b.is_empty() && b.len() <= 32 && b[0].is_ascii_lowercase() && b.iter().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'_')
65}
66
67pub fn is_name(s: &str) -> bool {
68 let b = s.as_bytes();
69 !b.is_empty() && b.len() <= 64 && b[0].is_ascii_alphanumeric() && b.iter().all(|c| c.is_ascii_alphanumeric() || *c == b'_' || *c == b'.' || *c == b'-')
70}
71
72fn is_lower_hex(s: &str, n: usize) -> bool {
73 s.len() == n && s.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
74}
75pub fn is_digest(s: &str) -> bool {
76 is_lower_hex(s, 64)
77}
78pub fn is_hex40(s: &str) -> bool {
79 is_lower_hex(s, 40)
80}
81/// <text>: 1..200 bytes of 0x20..0x7E, no leading or trailing space (byte rules already exclude other bytes).
82pub fn is_text200(s: &str) -> bool {
83 !s.is_empty() && s.len() <= 200 && !s.starts_with(' ') && !s.ends_with(' ')
84}
85pub fn is_path(s: &str) -> bool {
86 !s.is_empty() && s.len() <= 200 && s.bytes().all(|c| (0x21..=0x7e).contains(&c))
87}
88pub fn is_repo(s: &str) -> bool {
89 let ok = |p: &str| !p.is_empty() && p.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'_' || c == b'.' || c == b'-');
90 match s.split_once('/') {
91 Some((a, b)) => ok(a) && ok(b),
92 None => false,
93 }
94}
95/// YYYY-MM-DDTHH:MM:SSZ with calendar ranges.
96pub fn is_utc(s: &str) -> bool {
97 let b = s.as_bytes();
98 if b.len() != 20 || b[4] != b'-' || b[7] != b'-' || b[10] != b'T' || b[13] != b':' || b[16] != b':' || b[19] != b'Z' {
99 return false;
100 }
101 let num = |a: usize, n: usize| -> Option<u32> {
102 let t = &s[a..a + n];
103 if t.bytes().all(|c| c.is_ascii_digit()) { t.parse().ok() } else { None }
104 };
105 match (num(0, 4), num(5, 2), num(8, 2), num(11, 2), num(14, 2), num(17, 2)) {
106 (Some(_), Some(mo), Some(d), Some(h), Some(mi), Some(se)) => (1..=12).contains(&mo) && (1..=31).contains(&d) && h <= 23 && mi <= 59 && se <= 60,
107 _ => false,
108 }
109}
110pub fn is_code(s: &str) -> bool {
111 let b = s.as_bytes();
112 !b.is_empty() && b[0].is_ascii_uppercase() && b.iter().all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || *c == b'_')
113}
114
115pub const FAILURE_CODES: [&str; 12] = [
116 "BOUND_KIND_INSUFFICIENT",
117 "CONDITIONAL_UNDEFINED",
118 "CONSTRAINT_RESIDUAL_EXCEEDED",
119 "INTERACTING_DEVIATION_EXCEEDED",
120 "NONFINITE_VALUE",
121 "ORACLE_DISAGREEMENT",
122 "POVM_NORMALIZATION_EXCEEDED",
123 "PRECISION_INSUFFICIENT",
124 "PROBABILITY_OUT_OF_RANGE",
125 "PROBABILITY_SUM_EXCEEDED",
126 "SCHRODINGER_DEVIATION_EXCEEDED",
127 "TRIVIAL_PHYSICAL_STATE",
128];
129pub const RUN_ERROR_CODES: [&str; 3] = ["ORACLE_UNAVAILABLE", "RESOURCE_LIMIT", "INTERNAL_ERROR"];
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134 #[test]
135 fn byte_rules() {
136 assert!(lines_checked(b"a b\n").is_ok());
137 assert!(lines_checked(b"a b").is_err());
138 assert!(lines_checked(b"a b\n").is_err());
139 assert!(lines_checked(b"a b \n").is_err());
140 assert!(lines_checked(b"a\n\nb\n").is_err());
141 assert!(lines_checked(b"a\r\n").is_err());
142 assert!(lines_checked(b"a\tb\n").is_err());
143 assert!(lines_checked(b"").is_err());
144 let mut s = FAILURE_CODES.to_vec();
145 s.sort();
146 assert_eq!(s, FAILURE_CODES.to_vec(), "closed set is listed sorted bytewise");
147 assert!(is_utc("2026-10-10T02:00:00Z") && !is_utc("2026-13-10T02:00:00Z"));
148 assert!(is_repo("aien-dev/omega") && !is_repo("aien dev/omega"));
149 }
150}