+/TECS

git clone https://git.julia.blue/+/TECS

P1n3appl3Finish project 126e68fc9

v1.0
4.2 KiB162 linesraw
1/**
2 * Represents character strings. In addition for constructing and disposing
3 * strings, the class features methods for getting and setting individual
4 * characters of the string, for erasing the string's last character,
5 * for appending a character to the string's end, and more typical
6 * string-oriented operations.
7 */
8class String {
9    field Array contents;
10    field int len, maxLen;
11
12    /** constructs a new empty string with a maximum length of maxLength
13     *  and initial length of 0. */
14    constructor String new(int maxLength) {
15        if(maxLength < 0){
16            do Sys.error(14);
17        }
18        if(maxLength = 0){
19            let maxLength = maxLength + 1; // Allocating 0 bytes breaks malloc
20        }
21        let len = 0;
22        let maxLen = maxLength;
23        let contents = Array.new(maxLength);
24        return this;
25    }
26
27    /** Disposes this string. */
28    method void dispose() {
29        do contents.dispose();
30        do Memory.deAlloc(this);
31        return;
32    }
33
34    /** Returns the current length of this string. */
35    method int length() {
36        return len;
37    }
38
39    /** Returns the character at the j-th location of this string. */
40    method char charAt(int j) {
41        if((j < 0) | (j > len)){
42            do Sys.error(15);
43        }
44        return contents[j];
45    }
46
47    /** Sets the character at the j-th location of this string to c. */
48    method void setCharAt(int j, char c) {
49        if((j < 0) | (j > len)){
50            do Sys.error(16);
51        }
52        let contents[j] = c;
53        return;
54    }
55
56    /** Appends c to this string's end and returns this string. */
57    method String appendChar(char c) {
58        if(len = maxLen){
59            do Sys.error(17);
60        }
61        let contents[len] = c;
62        let len = len + 1;
63        return this;
64    }
65
66    /** Erases the last character from this string. */
67    method void eraseLastChar() {
68        if(len = 0){
69            do Sys.error(18);
70        }
71        let len = len - 1;
72        return;
73    }
74
75    /** Returns the integer value of this string,
76     *  until a non-digit character is detected. */
77     method int intValue() {
78         var int sum, i, temp;
79         var boolean negative;
80         var Array multiples;
81         let multiples = Array.new(5);
82         let i = 0;
83         let temp = 1;
84         while(i < 5){
85             let multiples[i] = temp;
86             let i = i + 1;
87             let temp = temp * 10;
88         }
89         let sum = 0;
90         let i = 0;
91         let temp = 0;
92         let negative = false;
93         if(contents[0] = 45){   //minus sign
94             let negative = true;
95             let i = 1;
96         }
97         while((i < len) & (contents[i] > 47) & (contents[i] < 58)){
98             let i = i + 1;
99         }
100         let temp = i;
101         let i = 0;
102         if(negative){
103             let i = 1;
104         }
105         while(i < temp){
106             let sum = sum + ((contents[i] - 48) * multiples[(temp - i) - 1]);
107             let i = i + 1;
108         }
109         if(negative){
110             let sum = -sum;
111         }
112         do multiples.dispose();
113         return sum;
114     }
115
116    /** Sets this string to hold a representation of the given value. */
117    method void setInt(int val) {
118        var int temp, n;
119        let len = 0;
120        if(val = 0){
121            let contents[0] = 48;
122            let len = 1;
123            return;
124        }
125        if(val < 0){
126            let val = -val;
127            do appendChar(45);
128        }
129        let temp = 1;
130        while((temp < val) & (temp < 10000)){
131            let temp = temp * 10;
132        }
133        if(temp > val){
134            let temp = temp / 10;
135        }
136        while(temp > 0){
137            if(len = maxLen){
138                do Sys.error(19);
139            }
140            let n = val / temp;
141            do appendChar(n + 48);
142            let val = val - (n * temp);
143            let temp = temp / 10;
144        }
145        return;
146    }
147
148    /** Returns the new line character. */
149    function char newLine() {
150        return 128;
151    }
152
153    /** Returns the backspace character. */
154    function char backSpace() {
155        return 129;
156    }
157
158    /** Returns the double quote (") character. */
159    function char doubleQuote() {
160        return 34;
161    }
162}