-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPSUtility.java
More file actions
176 lines (150 loc) · 5.05 KB
/
SPSUtility.java
File metadata and controls
176 lines (150 loc) · 5.05 KB
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
package SPS1620;
/*
* SPSUtility.java - utility routines to evaluate operands, manipulate strings, and validate data
*
* IBM 1620 Jr Project, Computer History Museum, 2017-2024
*
* To recreate the experience (visual, auditory, tactile, visceral) of running historic software on a 1960s-era computer.
*
* Dave Babcock - project lead, software, library
* John M. Bohn Jr. - software
* David Brock - CHM sponsor
* Steve Casner - hardware, software
* Joe Fredrick - hardware, firmware
* Len Shustek - CHM advisor
* Dag Spicer - CHM advisor
* David Wise - IBM 1620 expert
*/
public class SPSUtility {
// Methods
public static boolean IsEmpty(String str) {
if (str == null) return true;
if (str.length() == 0) return true;
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) != ' ') return false;
}
return true;
}
public static boolean IsRMGM(byte digit) {
byte val = (byte)(digit & SPSData.MASK_DIGIT);
if ((val == SPSData.MEMORY_RM) || (val == SPSData.MEMORY_GM)) return true;
return false;
}
public static String Substring(String str, int start) {
if (IsEmpty(str)) return "";
if (start >= str.length()) return "";
return str.substring(start, str.length());
}
public static String Substring(String str, int start, int end) {
if (IsEmpty(str)) return "";
if (start >= str.length()) return "";
return str.substring(start, Integer.min(end, str.length()));
}
public static boolean Contains(String str, String substr) {
if (IsEmpty(str)) return false;
return str.contains(substr);
}
public static String Strip(String str) {
if (IsEmpty(str)) return "";
String nstr = "";
for (int i = 0; i < str.length(); ++i) {
if ((str.charAt(i) != ' ') && (str.charAt(i) != '\t')) nstr += str.charAt(i);
}
return nstr;
}
public static String LTrim(String str) {
if (IsEmpty(str)) return "";
for (int i = 0; i < str.length(); ++i) {
if ((str.charAt(i) != ' ') && (str.charAt(i) != '\t')) return str.substring(i);
}
return "";
}
public static String RTrim(String str) {
if (IsEmpty(str)) return "";
for (int i = (str.length() - 1); i >= 0; --i) {
if ((str.charAt(i) != ' ') && (str.charAt(i) != '\t')) return str.substring(0, i + 1);
}
return "";
}
public static String Trim(String str) {
if (IsEmpty(str)) return "";
return str.trim();
}
public static String ExpandTabs(String str) {
if (IsEmpty(str)) return "";
if (SPSData.sourceFormat == SPSData.SourceFormat.FIXED) return str;
if (str.indexOf('\t') == -1) return str;
boolean single = (SPSData.tabTable.length == 1);
int tabs = SPSData.tabTable[0];
char[] chr = str.toCharArray();
String str2 = "";
for (int i = 0; i < chr.length; ++ i) {
if (chr[i] != '\t') {
str2 += chr[i];
} else if (single) {
int spaces = tabs - (str2.length() % tabs);
for (int j = 0; j < spaces; ++j) {
str2 += ' ';
}
} else {
int pos = str2.length();
int spaces = 1;
for (int j = 0; j < SPSData.tabTable.length; ++j) {
if (pos < SPSData.tabTable[j]) {
spaces = SPSData.tabTable[j] - pos - 1;
break;
}
}
for (int j = 0; j < spaces; ++j) {
str2 += ' ';
}
}
}
return str2;
}
public static char CharAt(String str, int pos) {
if ((str == null) || (pos < 0) || (pos >= str.length())) return '\0';
return str.charAt(pos);
}
public static boolean IsValidLine(String str) {
return SPSData.PATTERN_LINE.matcher(str).matches();
}
public static boolean IsValidHead(char head) {
return (SPSData.VALID_HEAD.indexOf(head) != -1);
}
public static boolean IsValidLabel(String str) {
if (SPSData.symbolDivide) {
return SPSData.PATTERN_LABEL.matcher(str).matches() &&
!SPSData.PATTERN_NUMBER.matcher(str).matches();
} else {
return SPSData.PATTERN_LABELX.matcher(str).matches() &&
!SPSData.PATTERN_NUMBER.matcher(str).matches();
}
}
public static boolean IsValidSymbol(String str) {
if (SPSData.symbolDivide) {
return (SPSData.PATTERN_SYMBOL.matcher(str).matches() ||
SPSData.PATTERN_SYMBOLH.matcher(str).matches()) &&
!SPSData.PATTERN_NUMBER.matcher(str).matches();
} else {
return (SPSData.PATTERN_SYMBOLX.matcher(str).matches() ||
SPSData.PATTERN_SYMBOLHX.matcher(str).matches()) &&
!SPSData.PATTERN_NUMBER.matcher(str).matches();
}
}
public static boolean IsValidNumber(String str) {
return SPSData.PATTERN_NUMBER.matcher(str).matches();
}
public static boolean IsValidNumberX(String str) {
return SPSData.PATTERN_NUMBERX.matcher(str).matches();
}
public static boolean IsValidAlpha(String str) {
return SPSData.PATTERN_ALPHA.matcher(str).matches();
}
public static boolean IsValidIndex(String str) {
return SPSData.PATTERN_INDEX.matcher(str).matches();
}
public static boolean IsValidFlags(String str) {
return SPSData.PATTERN_FLAGS.matcher(str).matches();
}
}