MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / is_numeric_literal

Function is_numeric_literal

src/code_actions/extract_constant.rs:70–132  ·  view source on GitHub ↗

Returns `true` when the text is a numeric literal (integer or float, including hex `0x`, octal `0o`, binary `0b`, and underscored forms).

(t: &str)

Source from the content-addressed store, hash-verified

68/// Returns `true` when the text is a numeric literal (integer or float,
69/// including hex `0x`, octal `0o`, binary `0b`, and underscored forms).
70fn is_numeric_literal(t: &str) -> bool {
71 if t.is_empty() {
72 return false;
73 }
74
75 let bytes = t.as_bytes();
76
77 // Hex: 0x1F, 0X1f
78 if bytes.len() >= 3
79 && bytes[0] == b'0'
80 && (bytes[1] == b'x' || bytes[1] == b'X')
81 && bytes[2..]
82 .iter()
83 .all(|b| b.is_ascii_hexdigit() || *b == b'_')
84 {
85 return true;
86 }
87
88 // Binary: 0b101
89 if bytes.len() >= 3
90 && bytes[0] == b'0'
91 && (bytes[1] == b'b' || bytes[1] == b'B')
92 && bytes[2..]
93 .iter()
94 .all(|b| *b == b'0' || *b == b'1' || *b == b'_')
95 {
96 return true;
97 }
98
99 // Octal: 0o77
100 if bytes.len() >= 3
101 && bytes[0] == b'0'
102 && (bytes[1] == b'o' || bytes[1] == b'O')
103 && bytes[2..]
104 .iter()
105 .all(|b| (b'0'..=b'7').contains(b) || *b == b'_')
106 {
107 return true;
108 }
109
110 // Decimal integer or float
111 let mut saw_dot = false;
112 let mut saw_e = false;
113 for (i, &b) in bytes.iter().enumerate() {
114 match b {
115 b'0'..=b'9' | b'_' => {}
116 b'.' if !saw_dot && !saw_e => saw_dot = true,
117 b'e' | b'E' if !saw_e && i > 0 => {
118 saw_e = true;
119 // Allow optional +/- after exponent
120 if i + 1 < bytes.len() && (bytes[i + 1] == b'+' || bytes[i + 1] == b'-') {
121 // Skip the sign — it will be consumed next iteration.
122 // We need a slightly different approach: just validate
123 // the whole thing.
124 return validate_float_suffix(&bytes[i + 1..]);
125 }
126 }
127 _ => return false,

Callers 3

is_extractable_literalFunction · 0.85
is_concat_expressionFunction · 0.85
generate_constant_nameFunction · 0.85

Calls 5

validate_float_suffixFunction · 0.85
iterMethod · 0.80
containsMethod · 0.80
is_emptyMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected