/** * Represents character strings. In addition for constructing and disposing * strings, the class features methods for getting and setting individual * characters of the string, for erasing the string's last character, * for appending a character to the string's end, and more typical * string-oriented operations. */ class String { field Array contents; field int len, maxLen; /** constructs a new empty string with a maximum length of maxLength * and initial length of 0. */ constructor String new(int maxLength) { if(maxLength < 0){ do Sys.error(14); } if(maxLength = 0){ let maxLength = maxLength + 1; // Allocating 0 bytes breaks malloc } let len = 0; let maxLen = maxLength; let contents = Array.new(maxLength); return this; } /** Disposes this string. */ method void dispose() { do contents.dispose(); do Memory.deAlloc(this); return; } /** Returns the current length of this string. */ method int length() { return len; } /** Returns the character at the j-th location of this string. */ method char charAt(int j) { if((j < 0) | (j > len)){ do Sys.error(15); } return contents[j]; } /** Sets the character at the j-th location of this string to c. */ method void setCharAt(int j, char c) { if((j < 0) | (j > len)){ do Sys.error(16); } let contents[j] = c; return; } /** Appends c to this string's end and returns this string. */ method String appendChar(char c) { if(len = maxLen){ do Sys.error(17); } let contents[len] = c; let len = len + 1; return this; } /** Erases the last character from this string. */ method void eraseLastChar() { if(len = 0){ do Sys.error(18); } let len = len - 1; return; } /** Returns the integer value of this string, * until a non-digit character is detected. */ method int intValue() { var int sum, i, temp; var boolean negative; var Array multiples; let multiples = Array.new(5); let i = 0; let temp = 1; while(i < 5){ let multiples[i] = temp; let i = i + 1; let temp = temp * 10; } let sum = 0; let i = 0; let temp = 0; let negative = false; if(contents[0] = 45){ //minus sign let negative = true; let i = 1; } while((i < len) & (contents[i] > 47) & (contents[i] < 58)){ let i = i + 1; } let temp = i; let i = 0; if(negative){ let i = 1; } while(i < temp){ let sum = sum + ((contents[i] - 48) * multiples[(temp - i) - 1]); let i = i + 1; } if(negative){ let sum = -sum; } do multiples.dispose(); return sum; } /** Sets this string to hold a representation of the given value. */ method void setInt(int val) { var int temp, n; let len = 0; if(val = 0){ let contents[0] = 48; let len = 1; return; } if(val < 0){ let val = -val; do appendChar(45); } let temp = 1; while((temp < val) & (temp < 10000)){ let temp = temp * 10; } if(temp > val){ let temp = temp / 10; } while(temp > 0){ if(len = maxLen){ do Sys.error(19); } let n = val / temp; do appendChar(n + 48); let val = val - (n * temp); let temp = temp / 10; } return; } /** Returns the new line character. */ function char newLine() { return 128; } /** Returns the backspace character. */ function char backSpace() { return 129; } /** Returns the double quote (") character. */ function char doubleQuote() { return 34; } }