From 764ac9449f82ef3df09671138179d91017793515 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Mon, 23 Feb 2026 14:14:36 +0000 Subject: [PATCH] feat: add uuid helper (#12500) --- codex-rs/Cargo.lock | 1 + codex-rs/utils/string/Cargo.toml | 3 +++ codex-rs/utils/string/src/lib.rs | 43 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index e68e85b40..a4bd8e47e 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2484,6 +2484,7 @@ name = "codex-utils-string" version = "0.0.0" dependencies = [ "pretty_assertions", + "regex-lite", ] [[package]] diff --git a/codex-rs/utils/string/Cargo.toml b/codex-rs/utils/string/Cargo.toml index 8a6253938..3f5890cfb 100644 --- a/codex-rs/utils/string/Cargo.toml +++ b/codex-rs/utils/string/Cargo.toml @@ -7,5 +7,8 @@ license.workspace = true [lints] workspace = true +[dependencies] +regex-lite = { workspace = true } + [dev-dependencies] pretty_assertions = { workspace = true } diff --git a/codex-rs/utils/string/src/lib.rs b/codex-rs/utils/string/src/lib.rs index b69f18564..df9ae59e1 100644 --- a/codex-rs/utils/string/src/lib.rs +++ b/codex-rs/utils/string/src/lib.rs @@ -62,11 +62,54 @@ pub fn sanitize_metric_tag_value(value: &str) -> String { } } +/// Find all UUIDs in a string. +#[allow(clippy::unwrap_used)] +pub fn find_uuids(s: &str) -> Vec { + static RE: std::sync::OnceLock = std::sync::OnceLock::new(); + let re = RE.get_or_init(|| { + regex_lite::Regex::new( + r"[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}", + ) + .unwrap() // Unwrap is safe thanks to the tests. + }); + + re.find_iter(s).map(|m| m.as_str().to_string()).collect() +} + #[cfg(test)] mod tests { + use super::find_uuids; use super::sanitize_metric_tag_value; use pretty_assertions::assert_eq; + #[test] + fn find_uuids_finds_multiple() { + let input = + "x 00112233-4455-6677-8899-aabbccddeeff-k y 12345678-90ab-cdef-0123-456789abcdef"; + assert_eq!( + find_uuids(input), + vec![ + "00112233-4455-6677-8899-aabbccddeeff".to_string(), + "12345678-90ab-cdef-0123-456789abcdef".to_string(), + ] + ); + } + + #[test] + fn find_uuids_ignores_invalid() { + let input = "not-a-uuid-1234-5678-9abc-def0-123456789abc"; + assert_eq!(find_uuids(input), Vec::::new()); + } + + #[test] + fn find_uuids_handles_non_ascii_without_overlap() { + let input = "🙂 55e5d6f7-8a7f-4d2a-8d88-123456789012abc"; + assert_eq!( + find_uuids(input), + vec!["55e5d6f7-8a7f-4d2a-8d88-123456789012".to_string()] + ); + } + #[test] fn sanitize_metric_tag_value_trims_and_fills_unspecified() { let msg = "///";