From 4762d6a0a75cfa6128f251077c7815198edb4e2b Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Thu, 18 Dec 2025 02:01:12 +0100 Subject: [PATCH] common: extract constants into crate Part of #12310 --- crates/common/src/lib.rs | 30 ++++++++++++++++++++++++++++++ src/common.rs | 33 ++++----------------------------- src/expand.rs | 8 ++++---- src/highlight/highlight.rs | 5 ++--- src/reader/reader.rs | 7 ++++--- src/wildcard.rs | 5 ++--- 6 files changed, 46 insertions(+), 42 deletions(-) diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index dffff0611..f13455326 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -3,6 +3,36 @@ use std::os::unix::ffi::OsStrExt; use std::sync::OnceLock; +pub const PACKAGE_NAME: &str = env!("CARGO_PKG_NAME"); + +// Highest legal ASCII value. +pub const ASCII_MAX: char = 127 as char; + +// Highest legal 16-bit Unicode value. +pub const UCS2_MAX: char = '\u{FFFF}'; + +// Highest legal byte value. +pub const BYTE_MAX: char = 0xFF as char; + +// Unicode BOM value. +pub const UTF8_BOM_WCHAR: char = '\u{FEFF}'; + +// Use Unicode "non-characters" for internal characters as much as we can. This +// gives us 32 "characters" for internal use that we can guarantee should not +// appear in our input stream. See http://www.unicode.org/faq/private_use.html. +pub const RESERVED_CHAR_BASE: char = '\u{FDD0}'; +pub const RESERVED_CHAR_END: char = '\u{FDF0}'; +// Split the available non-character values into two ranges to ensure there are +// no conflicts among the places we use these special characters. +pub const EXPAND_RESERVED_BASE: char = RESERVED_CHAR_BASE; +pub const EXPAND_RESERVED_END: char = char_offset(EXPAND_RESERVED_BASE, 16); +pub const WILDCARD_RESERVED_BASE: char = EXPAND_RESERVED_END; +pub const WILDCARD_RESERVED_END: char = char_offset(WILDCARD_RESERVED_BASE, 16); +// Make sure the ranges defined above don't exceed the range for non-characters. +// This is to make sure we didn't do something stupid in subdividing the +// Unicode range for our needs. +const _: () = assert!(WILDCARD_RESERVED_END <= RESERVED_CHAR_END); + // These are in the Unicode private-use range. We really shouldn't use this // range but have little choice in the matter given how our lexer/parser works. // We can't use non-characters for these two ranges because there are only 66 of diff --git a/src/common.rs b/src/common.rs index bdc21eb17..abd191561 100644 --- a/src/common.rs +++ b/src/common.rs @@ -16,7 +16,10 @@ use crate::wildcard::{ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE}; use crate::wutil::fish_iswalnum; use bitflags::bitflags; -use fish_common::{ENCODE_DIRECT_END, char_offset, is_console_session, subslice_position}; +use fish_common::{ + ASCII_MAX, BYTE_MAX, ENCODE_DIRECT_END, RESERVED_CHAR_BASE, RESERVED_CHAR_END, UCS2_MAX, + is_console_session, subslice_position, +}; use fish_fallback::fish_wcwidth; use fish_wchar::{decode_byte_from_char, encode_byte_to_char}; use libc::{SIG_IGN, SIGTTOU, STDIN_FILENO}; @@ -35,34 +38,6 @@ pub const PACKAGE_NAME: &str = env!("CARGO_PKG_NAME"); -// Highest legal ASCII value. -pub const ASCII_MAX: char = 127 as char; - -// Highest legal 16-bit Unicode value. -pub const UCS2_MAX: char = '\u{FFFF}'; - -// Highest legal byte value. -pub const BYTE_MAX: char = 0xFF as char; - -// Unicode BOM value. -pub const UTF8_BOM_WCHAR: char = '\u{FEFF}'; - -// Use Unicode "non-characters" for internal characters as much as we can. This -// gives us 32 "characters" for internal use that we can guarantee should not -// appear in our input stream. See http://www.unicode.org/faq/private_use.html. -pub const RESERVED_CHAR_BASE: char = '\u{FDD0}'; -pub const RESERVED_CHAR_END: char = '\u{FDF0}'; -// Split the available non-character values into two ranges to ensure there are -// no conflicts among the places we use these special characters. -pub const EXPAND_RESERVED_BASE: char = RESERVED_CHAR_BASE; -pub const EXPAND_RESERVED_END: char = char_offset(EXPAND_RESERVED_BASE, 16); -pub const WILDCARD_RESERVED_BASE: char = EXPAND_RESERVED_END; -pub const WILDCARD_RESERVED_END: char = char_offset(WILDCARD_RESERVED_BASE, 16); -// Make sure the ranges defined above don't exceed the range for non-characters. -// This is to make sure we didn't do something stupid in subdividing the -// Unicode range for our needs. -const _: () = assert!(WILDCARD_RESERVED_END <= RESERVED_CHAR_END); - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum EscapeStringStyle { Script(EscapeFlags), diff --git a/src/expand.rs b/src/expand.rs index dc4561d49..f120d9344 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -8,9 +8,9 @@ STATUS_INVALID_ARGS, STATUS_NOT_EXECUTABLE, STATUS_READ_TOO_MUCH, STATUS_UNMATCHED_WILDCARD, }; use crate::common::{ - EXPAND_RESERVED_BASE, EXPAND_RESERVED_END, EscapeFlags, EscapeStringStyle, UnescapeFlags, - UnescapeStringStyle, charptr2wcstring, escape, escape_string, escape_string_for_double_quotes, - unescape_string, valid_var_name_char, wcs2zstring, + EscapeFlags, EscapeStringStyle, UnescapeFlags, UnescapeStringStyle, charptr2wcstring, escape, + escape_string, escape_string_for_double_quotes, unescape_string, valid_var_name_char, + wcs2zstring, }; use crate::complete::{CompleteFlags, Completion, CompletionList, CompletionReceiver}; use crate::env::{EnvVar, Environment}; @@ -30,7 +30,7 @@ use crate::wildcard::{wildcard_expand_string, wildcard_has_internal}; use crate::wutil::{Options, normalize_path, wcstoi_partial}; use bitflags::bitflags; -use fish_common::char_offset; +use fish_common::{EXPAND_RESERVED_BASE, EXPAND_RESERVED_END, char_offset}; use std::mem::MaybeUninit; bitflags! { diff --git a/src/highlight/highlight.rs b/src/highlight/highlight.rs index bde670518..73838cff8 100644 --- a/src/highlight/highlight.rs +++ b/src/highlight/highlight.rs @@ -6,9 +6,7 @@ }; use crate::builtins::shared::builtin_exists; use crate::color::Color; -use crate::common::{ - ASCII_MAX, EXPAND_RESERVED_BASE, EXPAND_RESERVED_END, valid_var_name, valid_var_name_char, -}; +use crate::common::{valid_var_name, valid_var_name_char}; use crate::complete::complete_wrap_map; use crate::env::{EnvVar, Environment}; use crate::expand::{ @@ -32,6 +30,7 @@ use crate::threads::assert_is_background_thread; use crate::tokenizer::{PipeOrRedir, variable_assignment_equals_pos}; use crate::wcstringutil::string_prefixes_string; +use fish_common::{ASCII_MAX, EXPAND_RESERVED_BASE, EXPAND_RESERVED_END}; use fish_wchar::{L, WExt, WString, wstr}; use std::collections::HashMap; use std::collections::hash_map::Entry; diff --git a/src/reader/reader.rs b/src/reader/reader.rs index 117757f18..e3623f1b1 100644 --- a/src/reader/reader.rs +++ b/src/reader/reader.rs @@ -18,6 +18,7 @@ //! expansion, etc. use crate::portable_atomic::AtomicU64; +use fish_common::UTF8_BOM_WCHAR; use libc::{ _POSIX_VDISABLE, ECHO, EINTR, EIO, EISDIR, ENOTTY, EPERM, ESRCH, ICANON, ICRNL, IEXTEN, INLCR, IXOFF, IXON, O_NONBLOCK, O_RDONLY, ONLCR, OPOST, SIGINT, SIGTTIN, STDERR_FILENO, STDIN_FILENO, @@ -54,9 +55,9 @@ use crate::builtins::shared::STATUS_CMD_OK; use crate::common::ScopeGuarding; use crate::common::{ - EscapeFlags, EscapeStringStyle, ScopeGuard, UTF8_BOM_WCHAR, bytes2wcstring, escape, - escape_string, exit_without_destructors, get_ellipsis_char, get_obfuscation_read_char, - get_program_name, restore_term_foreground_process_group_for_exit, shell_modes, write_loop, + EscapeFlags, EscapeStringStyle, ScopeGuard, bytes2wcstring, escape, escape_string, + exit_without_destructors, get_ellipsis_char, get_obfuscation_read_char, get_program_name, + restore_term_foreground_process_group_for_exit, shell_modes, write_loop, }; use crate::complete::{ CompleteFlags, Completion, CompletionList, CompletionRequestOptions, complete, complete_load, diff --git a/src/wildcard.rs b/src/wildcard.rs index 769b2cc88..ca2b13cf0 100644 --- a/src/wildcard.rs +++ b/src/wildcard.rs @@ -1,6 +1,6 @@ // Enumeration of all wildcard types. -use fish_common::char_offset; +use fish_common::{WILDCARD_RESERVED_BASE, char_offset}; use libc::X_OK; use std::cmp::Ordering; use std::collections::HashSet; @@ -8,8 +8,7 @@ use std::sync::LazyLock; use crate::common::{ - UnescapeFlags, UnescapeStringStyle, WILDCARD_RESERVED_BASE, WSL, - is_windows_subsystem_for_linux, unescape_string, + UnescapeFlags, UnescapeStringStyle, WSL, is_windows_subsystem_for_linux, unescape_string, }; use crate::complete::{CompleteFlags, Completion, CompletionReceiver, PROG_COMPLETE_SEP}; use crate::expand::ExpandFlags;