gbf_core/
utils.rs

1#![deny(missing_docs)]
2
3use std::ascii::escape_default;
4
5/// A type representing a bytecode address.
6pub type Gs2BytecodeAddress = usize;
7
8/// At what length text should be truncated for operands.
9pub const OPERAND_TRUNCATE_LENGTH: usize = 100;
10
11/// A constant representing the current version of the software, in semver format.
12pub const VERSION: &str = env!("CARGO_PKG_VERSION");
13
14/// A constant representing the name of the software.
15pub const NAME: &str = env!("CARGO_PKG_NAME");
16
17/// A constant representing GBF green
18pub const GBF_GREEN: &str = "#98ff64";
19
20/// A constant representing GBF red
21pub const GBF_RED: &str = "#ff6464";
22
23/// A constant representing GBF blue
24pub const GBF_BLUE: &str = "#64b2ff";
25
26/// A constant representing GBF yellow
27pub const GBF_YELLOW: &str = "#ffd964";
28
29/// A constant representing GBF light gray
30pub const GBF_LIGHT_GRAY: &str = "#cdcdcd";
31
32/// A constant representing GBF dark gray
33pub const GBF_DARK_GRAY: &str = "#1e1e1e";
34
35/// Max iterations for the structure analysis
36pub const STRUCTURE_ANALYSIS_MAX_ITERATIONS: usize = 1000;
37
38/// Escapes a string using `std::ascii::escape_default`.
39///
40/// # Arguments
41/// * `input` - A value that can be converted into a `String`.
42///
43/// # Returns
44/// A new `String` where each character is escaped according to `escape_default`.
45pub fn escape_string<S>(input: S) -> String
46where
47    S: Into<String>,
48{
49    input
50        .into()
51        .bytes()
52        .flat_map(escape_default)
53        .map(char::from)
54        .collect()
55}
56
57/// Encodes a string for HTML.
58///
59/// # Arguments
60/// * `input` - A value that can be converted into a `String`.
61///
62/// # Returns
63/// A new `String` that is HTML encoded.
64pub fn html_encode<S>(input: S) -> String
65where
66    S: Into<String>,
67{
68    input
69        .into()
70        .replace("&", "&amp;")
71        .replace("<", "&lt;")
72        .replace(">", "&gt;")
73        .replace("\"", "&quot;")
74        .replace("'", "&#39;")
75}