1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! This module contains the Character Inclusion Domain (CI).
//!
//! This domain considers the characters of a string and distinguishes
//! between two scenarios which are stored in different HashSets.
//! - The first set contains characters that are certainly contained in
//!   the string.
//! - The second set contains characters that may be in the string.
//!
//! This distinction is made when two CI domains merge.
//! Furthermore, the CI domain does not preserve information about the order of characters.
//! The *Top* value of the CI domain stands for an empty set of certainly
//! contained characters and the whole alphabet of allowed characters for the possibly contained characters.
//!
//! The following presents an example which shows how the CI domain works:
//!  1. When a string is assigned to the CI domain its unique characters are stored in both
//!    sets. e.g. "Hello, World!" => ({H,e,l,o,',',' ',W,o,r,d}, {H,e,l,o,',',' ',W,o,r,d})
//!  2. When two strings are concatenated, the union of the two sets of the two domains is taken.
//!     e.g. "Hello, " + "World" => ({H,e,l,o,',',' '} v {W,o,r,d}, {H,e,l,o,',',' '} v {W,o,r,d})
//!  3. When two domains are merged, the intersection of the certain sets and the union of possible sets are taken.
//!     e.g. ({H,e,l,o,',',' '}, {H,e,l,o,',',' '}) v ({W,o,r,l,d}, {W,o,r,l,d}) => ({l,o}, {H,e,l,o,',',' ',W,o,r,d})

use std::{collections::BTreeSet, fmt};

use crate::prelude::*;
use std::fmt::Debug;

use super::{AbstractDomain, DomainInsertion, HasTop};

/// The `CharacterInclusionDomain` is a abstract domain describing the characters a string certainly has
/// and the characters a string may have.
///
/// The value comprises of a set of certainly contained characters and a set of possibly contained characters
/// while the *Top* value does not get any data. However, the *Top* value stands for an empty set of certainly
/// contained characters and the whole alphabet of allowed characters for the possibly contained characters.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
pub enum CharacterInclusionDomain {
    /// The *Top* value stands for an empty set of certainly contained characters and
    /// the whole alphabet of allowed characters for the possibly contained characters.
    Top,
    /// The set of certainly contained characters and a set of possibly contained characters
    Value((CharacterSet, CharacterSet)),
}

impl CharacterInclusionDomain {
    /// Unwraps the values from the Character Inclusion Domain
    pub fn unwrap_value(&self) -> (CharacterSet, CharacterSet) {
        match self {
            CharacterInclusionDomain::Value(value) => value.clone(),
            _ => panic!("Unexpected Character Inclusion type."),
        }
    }
}

impl DomainInsertion for CharacterInclusionDomain {
    /// Append string domain as part of a concatenation. (different to merge)
    fn append_string_domain(&self, string_domain: &Self) -> CharacterInclusionDomain {
        match self {
            CharacterInclusionDomain::Value((self_certain, self_possible)) => match string_domain {
                CharacterInclusionDomain::Value((other_certain, other_possible)) => {
                    CharacterInclusionDomain::Value((
                        self_certain.union(other_certain.clone()),
                        self_possible.union(other_possible.clone()),
                    ))
                }
                CharacterInclusionDomain::Top => {
                    CharacterInclusionDomain::Value((self_certain.clone(), CharacterSet::Top))
                }
            },
            CharacterInclusionDomain::Top => match string_domain {
                CharacterInclusionDomain::Value((other_certain, _)) => {
                    CharacterInclusionDomain::Value((other_certain.clone(), CharacterSet::Top))
                }
                CharacterInclusionDomain::Top => CharacterInclusionDomain::Top,
            },
        }
    }

    /// Create a string domain that approximates float values.
    fn create_float_value_domain() -> Self {
        let float_character_set: BTreeSet<char> = vec![
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', 'a', 'i', 'n', 'f', 'e',
            'E',
        ]
        .into_iter()
        .collect();
        CharacterInclusionDomain::Value((
            CharacterSet::Value(vec![].into_iter().collect()),
            CharacterSet::Value(float_character_set),
        ))
    }

    /// Create a string domain that approximates char values.
    fn create_char_domain() -> Self {
        CharacterInclusionDomain::Top
    }

    /// Create a string domain that approximates integer values.
    fn create_integer_domain() -> Self {
        let integer_character_set: BTreeSet<char> =
            vec!['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-']
                .into_iter()
                .collect();
        CharacterInclusionDomain::Value((
            CharacterSet::Value(vec![].into_iter().collect()),
            CharacterSet::Value(integer_character_set),
        ))
    }

    /// Create a string domain that approximates pointer values.
    fn create_pointer_value_domain() -> Self {
        CharacterInclusionDomain::Top
    }

    /// Creates a top value of the domain.
    fn create_top_value_domain() -> Self {
        CharacterInclusionDomain::Top
    }

    /// Create a string domain that represents an empty string.
    fn create_empty_string_domain() -> Self {
        CharacterInclusionDomain::from("".to_string())
    }
}

impl From<String> for CharacterInclusionDomain {
    fn from(string: String) -> Self {
        let characters: BTreeSet<char> = string.chars().collect();
        CharacterInclusionDomain::Value((
            CharacterSet::Value(characters.clone()),
            CharacterSet::Value(characters),
        ))
    }
}

impl AbstractDomain for CharacterInclusionDomain {
    /// Merge two values; Takes the intersection of the certainly contained characters
    /// and the union of the possibly contained characters.
    /// Returns *Top* if either Domain represents it.
    fn merge(&self, other: &Self) -> Self {
        if self.is_top() || other.is_top() {
            Self::Top
        } else if self == other {
            self.clone()
        } else {
            let (self_certain, self_possible) = self.unwrap_value();
            let (other_certain, other_possible) = other.unwrap_value();
            Self::Value((
                self_certain.intersection(other_certain),
                self_possible.union(other_possible),
            ))
        }
    }

    /// Check if the value is *Top*.
    fn is_top(&self) -> bool {
        matches!(self, Self::Top)
    }
}

impl HasTop for CharacterInclusionDomain {
    /// Return a *Top* value
    fn top(&self) -> Self {
        CharacterInclusionDomain::Top
    }
}

impl fmt::Display for CharacterInclusionDomain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CharacterInclusionDomain::Top => write!(f, "Top"),
            CharacterInclusionDomain::Value((certain_set, possible_set)) => {
                write!(f, "Certain: {certain_set}, Possible: {possible_set}")
            }
        }
    }
}

/// A domain that represents character sets.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
pub enum CharacterSet {
    /// The *Top* value represents a character set of all allowed characters.
    Top,
    /// Represents a real subset of all allowed characters.
    Value(BTreeSet<char>),
}

impl CharacterSet {
    /// Unwraps the values from the CharacterSet
    pub fn unwrap_value(&self) -> BTreeSet<char> {
        match self {
            CharacterSet::Value(value) => value.clone(),
            _ => panic!("Unexpected CharacterSet type."),
        }
    }

    /// Takes the intersection of two character sets.
    /// None of the sets should be *Top* since otherwise
    /// the whole CharacterInclusionDomain would be *Top*
    /// which is checked beforehand.
    pub fn intersection(&self, other: Self) -> Self {
        if self.is_top() || other.is_top() {
            panic!("Unexpected Top Value for CharacterSet intersection.")
        }
        CharacterSet::Value(
            self.unwrap_value()
                .intersection(&other.unwrap_value())
                .cloned()
                .collect(),
        )
    }

    /// Takes the union of two character sets.
    /// If either of them is *Top* the union is *Top*.
    /// Otherwise the standard set union is taken.
    pub fn union(&self, other: Self) -> Self {
        if self.is_top() || other.is_top() {
            return CharacterSet::Top;
        }

        CharacterSet::Value(
            self.unwrap_value()
                .union(&other.unwrap_value())
                .cloned()
                .collect(),
        )
    }

    /// Check if the value is *Top*.
    fn is_top(&self) -> bool {
        matches!(self, Self::Top)
    }
}

impl HasTop for CharacterSet {
    /// Return a *Top* value
    fn top(&self) -> Self {
        CharacterSet::Top
    }
}

impl fmt::Display for CharacterSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CharacterSet::Top => write!(f, "Top"),
            CharacterSet::Value(char_set) => {
                write!(f, "{char_set:?}")
            }
        }
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;

    impl CharacterInclusionDomain {
        pub fn ci(concrete: &str) -> CharacterInclusionDomain {
            let abstract_set = CharacterSet::Value(concrete.chars().into_iter().collect());
            CharacterInclusionDomain::Value((abstract_set.clone(), abstract_set.clone()))
        }
    }

    #[test]
    fn merging() {
        let first = CharacterInclusionDomain::ci("abc");
        let second = CharacterInclusionDomain::ci("def");
        let third = CharacterInclusionDomain::ci("dabc");
        let possible_set = CharacterSet::Value("abcdef".chars().into_iter().collect());
        let certain_set = CharacterSet::Value("d".chars().into_iter().collect());

        assert_eq!(
            first.merge(&second),
            CharacterInclusionDomain::Value((
                CharacterSet::Value(BTreeSet::new()),
                possible_set.clone()
            ))
        );
        assert_eq!(
            third.merge(&second),
            CharacterInclusionDomain::Value((certain_set, possible_set))
        );
        assert_eq!(
            first.merge(&CharacterInclusionDomain::Top),
            CharacterInclusionDomain::Top
        );
        assert_eq!(
            CharacterInclusionDomain::Top.merge(&CharacterInclusionDomain::Top),
            CharacterInclusionDomain::Top
        );
    }
}