.
This commit is contained in:
Executable
+148
@@ -0,0 +1,148 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/12/String.jack
|
||||
|
||||
/**
|
||||
* 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 str;
|
||||
field int length;
|
||||
static bool neg;
|
||||
|
||||
/** constructs a new empty string with a maximum length of maxLength
|
||||
* and initial length of 0. */
|
||||
constructor String new(int maxLength) {
|
||||
if (maxLength > 0){
|
||||
let str = Array.new(maxLength);
|
||||
}
|
||||
let length = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Disposes this string. */
|
||||
method void dispose() {
|
||||
if(length > 0){
|
||||
do str.dispose();
|
||||
}
|
||||
do Memory.deAlloc(this);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Returns the current length of this string. */
|
||||
method int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
/** Returns the character at the j-th location of this string. */
|
||||
method char charAt(int j) {
|
||||
if(j > length){
|
||||
do Sys.error(7);
|
||||
}
|
||||
return str[j];
|
||||
}
|
||||
|
||||
/** Sets the character at the j-th location of this string to c. */
|
||||
method void setCharAt(int j, char c) {
|
||||
if(j > length){
|
||||
do Sys.error(7);
|
||||
}
|
||||
let str[j] = c;
|
||||
return;
|
||||
}
|
||||
|
||||
/** Appends c to this string's end and returns this string. */
|
||||
method String appendChar(char c) {
|
||||
let str[length] = c;
|
||||
let length = length + 1;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Erases the last character from this string. */
|
||||
method void eraseLastChar() {
|
||||
let str[length] = 0;
|
||||
let length = length - 1;
|
||||
return;
|
||||
}
|
||||
|
||||
/** Returns the integer value of this string,
|
||||
* until a non-digit character is detected. */
|
||||
method int intValue() {
|
||||
var int d, sum, i;
|
||||
var bool f;
|
||||
while(i < length){
|
||||
let d = str[i]; // kak da go zavyrtq v realen int
|
||||
if ((d > 47) & (d < 58)){
|
||||
let sum = (sum * 10) + (d - 48);
|
||||
}
|
||||
else{
|
||||
if((i = 0) & (d = 45)){
|
||||
let f = true;
|
||||
}
|
||||
else{
|
||||
let i = length;
|
||||
}
|
||||
}
|
||||
let i = i + 1;
|
||||
}
|
||||
if (f){
|
||||
return -sum;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/** Sets this string to hold a representation of the given value. */
|
||||
method void setInt(int val) {
|
||||
var int lastDigit, a,i;
|
||||
var char c;
|
||||
if(val < 0){
|
||||
let neg = true;
|
||||
let val = -val;
|
||||
}
|
||||
let a = val / 10;
|
||||
let lastDigit = val - (a * 10);
|
||||
let i = 48;
|
||||
while (i < 58){
|
||||
if (i = (48 + lastDigit)){
|
||||
let c = i;
|
||||
let i = 59;
|
||||
}
|
||||
let i = i + 1;
|
||||
}
|
||||
if (val < 10){
|
||||
let length = 0;
|
||||
if (neg){
|
||||
let neg = false;
|
||||
do this.appendChar(45);
|
||||
}
|
||||
|
||||
do this.appendChar(c);
|
||||
return;
|
||||
}
|
||||
else{
|
||||
do this.setInt(a);
|
||||
do this.appendChar(c);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user