-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChar.vdmsl
157 lines (124 loc) · 4.8 KB
/
Char.vdmsl
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
/*
A module that specifies and defines general purpose types, constants and functions over
characters and strings (sequences of characters).
All functions are explicit and executable. Where a non-executable condition adds value, it
is included as a comment.
*/
module Char
imports from Seq all
exports types struct Upper
struct Lower
struct Letter
struct Digit
struct Octal
struct Hex
struct AlphaNumUpper
struct AlphaNumLower
struct AlphaNum
struct Space
struct WhiteSpace
struct Phrase
struct PhraseUpper
struct PhraseLower
struct Text
struct TextUpper
struct TextLower
values SP, TB, CR, LF: char
WHITE_SPACE, UPPER, LOWER, LETTER, DIGIT, OCTAL, HEX, ALPHANUMUPPER, ALPHANUMLOWER, ALPHANUM, PUNCTUATION: set of char
UPPERS, LOWERS, LETTERS, DIGITS, OCTALS, HEXS, ALPHANUMUPPERS, ALPHANUMLOWERS, ALPHANUMS, PUNCTUATIONS: seq of char
functions toLower: Upper +> Lower
toUpper: Lower +> Upper
isDigit: char +> bool
isDigits: seq of char +> bool
isWhiteSpace: char +> bool
isWhiteSpaces: seq of char +> bool
trimWhite: seq of char +> seq of char
filterWhite: seq of char +> seq of char
definitions
types
Upper = char
inv c == c in set UPPER;
Lower = char
inv c == c in set LOWER;
Letter = char
inv c == c in set LETTER;
Digit = char
inv c == c in set DIGIT;
Octal = char
inv c == c in set OCTAL;
Hex = char
inv c == c in set HEX;
AlphaNumUpper = char
inv c == c in set ALPHANUMUPPER;
AlphaNumLower = char
inv c == c in set ALPHANUMLOWER;
AlphaNum = char
inv c == c in set ALPHANUM;
Space = char
inv c == c = SP;
WhiteSpace = char
inv ws == ws in set WHITE_SPACE;
Punctuation = char
inv c == c in set PUNCTUATION;
Phrase = seq1 of (AlphaNum|Space);
PhraseUpper = seq1 of (AlphaNumUpper|Space);
PhraseLower = seq1 of (AlphaNumLower|Space);
Text = seq1 of (AlphaNum|WhiteSpace|Punctuation);
TextUpper = seq1 of (AlphaNumUpper|WhiteSpace|Punctuation);
TextLower = seq1 of (AlphaNumLower|WhiteSpace|Punctuation);
values
SP:char = ' ';
TB:char = '\t';
CR:char = '\r';
LF:char = '\n';
WHITE_SPACE:set of char = {SP,TB,CR,LF};
UPPER:set of char = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
'R','S','T','U','V','W','X','Y','Z'};
UPPERS: seq of Upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
LOWER:set of char = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z'};
LOWERS: seq of Lower = "abcdefghijklmnopqrstuvwxyz";
LETTER:set of char = UPPER union LOWER;
LETTERS:seq of char = UPPERS ^ LOWERS;
DIGIT:set of char = {'0','1','2','3','4','5','6','7','8','9'};
DIGITS:seq of Digit = "0123456789";
ALPHANUMUPPER:set of char = UPPER union DIGIT;
ALPHANUMUPPERS:seq of char = UPPERS ^ DIGITS;
ALPHANUMLOWER:set of char = LOWER union DIGIT;
ALPHANUMLOWERS:seq of char = LOWERS ^ DIGITS;
ALPHANUM:set of char = LETTER union DIGIT;
ALPHANUMS:seq of char = LETTERS ^ DIGITS;
OCTAL:set of char = {'0','1','2','3','4','5','6','7'};
OCTALS:seq of Octal = "01234567";
HEX:set of char = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
HEXS:seq of Hex = "0123456789ABCDEF";
PUNCTUATION:set of char = {',','.',';',':','-','/'};
PUNCTUATIONS: seq of Punctuation = ",.;:-/";
functions
-- Convert upper case letter to lower case.
toLower: Upper +> Lower
toLower(c) == LOWERS(Seq`indexOf[Upper](c,UPPERS))
post toUpper(RESULT) = c;
-- Convert lower case letter to upper case.
toUpper: Lower +> Upper
toUpper(c) == UPPERS(Seq`indexOf[Lower](c,LOWERS));
--post toLower(RESULT) = c;
-- Is a character a decimal digit?
isDigit: char +> bool
isDigit(c) == c in set DIGIT;
-- Are all characters in a sequence decimal digits?
isDigits: seq of char +> bool
isDigits(sc) == forall c in set elems sc & isDigit(c);
-- Is a character white space?
isWhiteSpace: char +> bool
isWhiteSpace(c) == c in set WHITE_SPACE;
-- Are all characters in a sequence white space?
isWhiteSpaces: seq of char +> bool
isWhiteSpaces(sc) == forall c in set elems sc & isWhiteSpace(c);
-- Trim white space from the front and back of a string.
trimWhite: seq of char +> seq of char
trimWhite(s) == Seq`dropWhile[char](isWhiteSpace, reverse(Seq`dropWhile[char](isWhiteSpace, reverse(s))));
-- Filter white space from a string.
filterWhite: seq of char +> seq of char
filterWhite(s) == [ c | c in seq s & not isWhiteSpace(c) ];
end Char