Browse Source

Removed string support from upper() and lower()

Steven Hall 8 years ago
parent
commit
ccfb13885d

+ 2 - 7
docs/types.html

@@ -119,13 +119,8 @@
 	n = b.upper()           // n is now "HELLO WORLD"
 	n = b.upper()           // n is now "HELLO WORLD"
 	n = b.lower()           // n is now "hello world"
 	n = b.lower()           // n is now "hello world"
 
 
-	// upper() and lower() can both take multiple string or integer arguments
-	n = b.upper("He", "d")  // n is now "HEllo WordD"
-	n = b.lower(-1, -5)     // n is now "Hello world"
-
-	// upper() and lower() can also take multiple mixed arguments
-	n = b.upper("He", -1)   // n is now "HEllo WordD"
-	n = b.lower(-1, "W")    // n is now "Hello world"
+	// upper() and lower() can both take multiple integer arguments
+	n = b.upper(1, -1)     // n is now "HEllo WorlD"
 
 
 	// You are also able to edit strings by character...
 	// You are also able to edit strings by character...
 	b[0] = "Z"              // b is now "Zello World"
 	b[0] = "Z"              // b is now "Zello World"

+ 18 - 100
src/runtime/gravity_core.c

@@ -1535,82 +1535,13 @@ static bool string_repeat (gravity_vm *vm, gravity_value_t *args, uint16_t nargs
 	RETURN_VALUE(VALUE_FROM_CSTRING(vm, new_str), rindex);
 	RETURN_VALUE(VALUE_FROM_CSTRING(vm, new_str), rindex);
 }
 }
 
 
-
-// Returns -1 for out of bounds error
-// Returns -2 if we exit the loop wrong.
-// Returns 0 otherwise
-// Also return function arguments: index and ret
-// index: will help us to print out a runtime error if we go out of bounds
-// ret: is the returned string that our upper() and lower() methods return.
-static int parse_arguments_for_upper_and_lower(gravity_vm *vm, int (*is_case)(int), int (*to_case)(int), gravity_value_t value, int32_t *index, char *ret, int ret_len) {
-	// If argument is an integer, capitalize the character specified by that integer
-	if (VALUE_ISA_INT(value)) {
-		*index = (int32_t)VALUE_AS_INT(value);
-
-		if (*index < 0) *index = ret_len + *index;
-		if ((*index < 0) || ((uint32_t)*index >= ret_len)) return -1;
-
-		ret[*index] = to_case(ret[*index]);
-	}
-	// If argument is a string, capitalize the characters specified by that string
-	else if (VALUE_ISA_STRING(value)) {
-		gravity_string_t *search_str = VALUE_AS_STRING(value);
-
-		// Check if all of search_string is already the case we want
-		bool is_all_correct_case = true;
-		for (int j = 0; j < search_str->len; ++j) {
-		 	// Better also make sure that it is an alpha character.
-			if (!is_case(search_str->s[j]) && isalpha(search_str->s[j])) {
-				is_all_correct_case = false;
-				break;
-			}
-		}
-
-		// If it is all the correct case already, go to the next argument
-		if (is_all_correct_case) {
-			return 0;
-		}
-
-		// Otherwise, parse it, and change the case
-		int iterations = 0;
-		bool isNotInfiniteLoop;
-		do {
-			char *ptr = strstr(ret, search_str->s);
-
-			if (ptr == NULL) {
-				break;
-			}
-
-			int match_index_ret = ptr - ret;
-
-			for (int j = match_index_ret; j < match_index_ret + search_str->len; ++j) {
-				if (is_case(ret[j])) {
-				 // Skip if it is already the correct case
-				 continue;
-				}
-				ret[j] = to_case(ret[j]);
-			}
-			++iterations;
-		} while(isNotInfiniteLoop = iterations < ret_len);
-								// Breaks out when no matches are found
-								// We should never break out of this loop by this while
-								// condition, as we should break out once we have no more
-								// matches (when ptr == NULL). This is just a fail safe.
-		if (!isNotInfiniteLoop) {
-			return -2;
-		}
-	}
-	return 0;
-}
-
 static bool string_upper (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
 static bool string_upper (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
 	gravity_string_t *main_str = VALUE_AS_STRING(GET_VALUE(0));
 	gravity_string_t *main_str = VALUE_AS_STRING(GET_VALUE(0));
 
 
 	char ret[main_str->len + 1];
 	char ret[main_str->len + 1];
 	strcpy(ret, main_str->s);
 	strcpy(ret, main_str->s);
-	int err;
 
 
-	// if no arguments passed, change case of the whole string
+	// if no arguments passed, change the whole string to uppercase
 	if (nargs == 1) {
 	if (nargs == 1) {
 		for (int i = 0; i <= main_str->len; i++) {
 		for (int i = 0; i <= main_str->len; i++) {
 		 ret[i] = toupper(ret[i]);
 		 ret[i] = toupper(ret[i]);
@@ -1620,22 +1551,16 @@ static bool string_upper (gravity_vm *vm, gravity_value_t *args, uint16_t nargs,
 	else {
 	else {
 		for (int i = 1; i < nargs; ++i) {
 		for (int i = 1; i < nargs; ++i) {
 			gravity_value_t value = GET_VALUE(i);
 			gravity_value_t value = GET_VALUE(i);
-			int32_t index = (int32_t)VALUE_AS_INT(value);
-			// if the argument is a string or int, proceed, otherwise we throw a
-			// runtime error
-			if (VALUE_ISA_STRING(value) || VALUE_ISA_INT(value)) {
-			 // new string is returned by the "ret" argument
-				err = parse_arguments_for_upper_and_lower(vm, isupper, toupper, value, &index, ret, strlen(ret));
+			if (VALUE_ISA_INT(value)) {
+				int32_t index = (int32_t)VALUE_AS_INT(value);
+
+				if (index < 0) index = main_str->len + index;
+				if ((index < 0) || ((uint32_t)index >= main_str->len)) RETURN_ERROR("Out of bounds error: index %d beyond bounds 0...%d", index, main_str->len-1);
+
+				ret[index] = toupper(ret[index]);
 			}
 			}
 			else {
 			else {
-				RETURN_ERROR("upper() expects either no arguments, or arguments that are Strings or Ints only.");
-			}
-			// If the parse through a -1 error, the argument was ou of bounds
-			if (err == -1) {
-				RETURN_ERROR("Out of bounds error: index %d beyond bounds 0...%d", index, main_str->len-1);
-			}
-			else if (err == -2) {
-				RETURN_ERROR("search parse ran into an infinite loop");
+				RETURN_ERROR("upper() expects either no arguments, or integer arguments.");
 			}
 			}
 		}
 		}
 	}
 	}
@@ -1647,9 +1572,8 @@ static bool string_lower (gravity_vm *vm, gravity_value_t *args, uint16_t nargs,
 
 
 	char ret[main_str->len + 1];
 	char ret[main_str->len + 1];
 	strcpy(ret, main_str->s);
 	strcpy(ret, main_str->s);
-	int err;
 
 
-	// if no arguments passed, change case of the whole string
+	// if no arguments passed, change the whole string to lowercase
 	if (nargs == 1) {
 	if (nargs == 1) {
 		for (int i = 0; i <= main_str->len; i++) {
 		for (int i = 0; i <= main_str->len; i++) {
 		 ret[i] = tolower(ret[i]);
 		 ret[i] = tolower(ret[i]);
@@ -1659,22 +1583,16 @@ static bool string_lower (gravity_vm *vm, gravity_value_t *args, uint16_t nargs,
 	else {
 	else {
 		for (int i = 1; i < nargs; ++i) {
 		for (int i = 1; i < nargs; ++i) {
 			gravity_value_t value = GET_VALUE(i);
 			gravity_value_t value = GET_VALUE(i);
-			int32_t index = (int32_t)VALUE_AS_INT(value);
-			// if the argument is a string or int, proceed, otherwise we throw a
-			// runtime error
-			if (VALUE_ISA_STRING(value) || VALUE_ISA_INT(value)) {
-			 // new string is returned by the "ret" argument
-				err = parse_arguments_for_upper_and_lower(vm, islower, tolower, value, &index, ret, strlen(ret));
+			if (VALUE_ISA_INT(value)) {
+				int32_t index = (int32_t)VALUE_AS_INT(value);
+
+				if (index < 0) index = main_str->len + index;
+				if ((index < 0) || ((uint32_t)index >= main_str->len)) RETURN_ERROR("Out of bounds error: index %d beyond bounds 0...%d", index, main_str->len-1);
+
+				ret[index] = tolower(ret[index]);
 			}
 			}
 			else {
 			else {
-				RETURN_ERROR("lower() expects either no arguments, or arguments that are Strings or Ints only.");
-			}
-			// If the parse through a -1 error, the argument was ou of bounds
-			if (err == -1) {
-				RETURN_ERROR("Out of bounds error: index %d beyond bounds 0...%d", index, main_str->len-1);
-			}
-			else if (err == -2) {
-				RETURN_ERROR("search parse ran into an infinite loop");
+				RETURN_ERROR("lower() expects either no arguments, or integer arguments.");
 			}
 			}
 		}
 		}
 	}
 	}

+ 0 - 17
test/string/lower_method.gravity

@@ -7,23 +7,6 @@
 func main () {
 func main () {
 	var s = "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much VARIATION AS POSSIBLE"
 	var s = "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much VARIATION AS POSSIBLE"
 
 
-	var a_str = s.lower("V") == "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much vARIATION AS POSSIBLE"
-	if (a_str == false) { System.print("a) string failed\n"); return false }
-
-	var b_str = s.lower("I") == "tHiS iS JUST A REALLY LONG TEST STRiNG TO TRY AND GET AS much VARiATiON AS POSSiBLE"
-	if (a_str == false) { System.print("b) string failed\n"); return false }
-
-	var c_str = s.lower("IS") == "tHis is JUST A REALLY LONG TEST STRING TO TRY AND GET AS much VARIATION AS POSSIBLE"
-	if (a_str == false) { System.print("c) string failed\n"); return false }
-
-	// Try a character that is already lower
-	var d_str = s.lower("h V") == "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much vARIATION AS POSSIBLE"
-	if (a_str == false) { System.print("d) string failed\n"); return false }
-
-	var e_str = s.lower("muc") == "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much VARIATION AS POSSIBLE"
-	if (a_str == false) { System.print("e) string failed\n"); return false }
-
-
 	// Numbers Only
 	// Numbers Only
 	// The V in VARIATION
 	// The V in VARIATION
 	var a_int = s.lower(62) == "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much vARIATION AS POSSIBLE"
 	var a_int = s.lower(62) == "tHIS IS JUST A REALLY LONG TEST STRING TO TRY AND GET AS much vARIATION AS POSSIBLE"

+ 2 - 39
test/string/upper_lower_method_non_alpha.gravity

@@ -18,46 +18,9 @@ func main () {
 	var c = s.upper(5) == s
 	var c = s.upper(5) == s
 	if (c == false) { System.print("c) failed"); return false }
 	if (c == false) { System.print("c) failed"); return false }
 
 
-	var d = s.upper(",") == s
-	if (d == false) { System.print("d) failed"); return false }
-
 	// Single integer or string -- lower
 	// Single integer or string -- lower
-	var e = s.lower(5) == s
-	if (e == false) { System.print("e) failed"); return false }
-
-	var f = s.lower(",") == s
-	if (f == false) { System.print("f) failed"); return false }
-
-
-	// Longer strings -- upper
-	var g = s.upper("o,") == "HellO, World!"
-	if (g == false) { System.print("g) failed"); return false }
-
-	// Longer strings -- lower
-	var h = s.lower("o,") == s
-	if (h == false) { System.print("h) failed"); return false }
-
-	s = "HELLO, WORLD!"
-	var i = s.upper("O,") == s
-	if (i == false) { System.print("i) failed"); return false }
-
-	var j = s.lower("O,") == "HELLo, WORLD!"
-	if (j == false) { System.print("j) failed"); return false }
-
-	s = ","
-	var k = s.upper(",") == s
-	if (k == false) { System.print("k) failed"); return false }
-
-	var l = s.lower(",") == s
-	if (l == false) { System.print("l) failed"); return false }
-
-
-	s = ",!?:|"
-	var m = s.upper("!?") == s
-	if (m == false) { System.print("m) failed"); return false }
-
-	var n = s.lower("!?") == s
-	if (n == false) { System.print("m) failed"); return false }
+	var d = s.lower(5) == s
+	if (d == false) { System.print("d) failed"); return false }
 
 
 	return true
 	return true
 }
 }

+ 6 - 19
test/string/upper_lower_multiple_args.gravity

@@ -5,37 +5,24 @@
 };
 };
 
 
 func main () {
 func main () {
-	var s = "This is just a really long test string to try and get as MUCH variation as possible"
+	var s = "Hello World"
 
 
 	// UPPER
 	// UPPER
-	var a = s.upper("v", 1) == "THis is just a really long test string to try and get as MUCH Variation as possible"
+	var a = s.upper(1, 2, 3) == "HELLo World"
 	if (a == false) { System.print("a) upper failed"); return false }
 	if (a == false) { System.print("a) upper failed"); return false }
 
 
 	// Try swappings the order
 	// Try swappings the order
-	var b = s.upper(1, "v") == s.upper("v", 1)
+	var b = s.upper(3, 2, 1) == s.upper(1, 2, 3)
 	if (b == false) { System.print("b) upper failed"); return false }
 	if (b == false) { System.print("b) upper failed"); return false }
 
 
-	var c = s.upper("really", "MUCH", -1 "tr") == "This is just a REALLY long test sTRing to TRy and get as MUCH variation as possiblE"
-	if (c == false) { System.print("c) upper failed"); return false }
-
-	// Try a different order
-	var d = s.upper(-1, "tr", "really", "MUCH") == s.upper("really", "MUCH", -1 "tr")
-	if (d == false) { System.print("d) upper failed"); return false }
-
 
 
+	s = "HELLO WORLD"
 	// LOWER
 	// LOWER
-	a = s.lower("M", 0) == "this is just a really long test string to try and get as mUCH variation as possible"
+	a = s.lower(0, 2, 4) == "hElLo WORLD"
 	if (a == false) { System.print("a) lower failed"); return false }
 	if (a == false) { System.print("a) lower failed"); return false }
 
 
-	b = s.lower(0, "M") == s.lower("M", 0)
+	b = s.lower(4, 2, 0) == s.lower(0, 2, 4)
 	if (b == false) { System.print("b) lower failed"); return false }
 	if (b == false) { System.print("b) lower failed"); return false }
 
 
-	c = s.lower("REALLY", "MU", -83 "H") == "this is just a really long test string to try and get as muCh variation as possible"
-	if (c == false) { System.print("c) lower failed"); return false }
-
-	// Try a different order
-	d = s.lower(-83, "H", "REALLY", "MU") == s.lower("REALLY", "MU", -83 "H")
-	if (d == false) { System.print("d) lower failed"); return false }
-
 	return true
 	return true
 }
 }

+ 0 - 21
test/string/upper_method.gravity

@@ -7,27 +7,6 @@
 func main () {
 func main () {
 	var s = "This is just a really long test string to try and get as MUCH variation as possible"
 	var s = "This is just a really long test string to try and get as MUCH variation as possible"
 
 
-	// 1 match
-	var a_str = s.upper("v") == "This is just a really long test string to try and get as MUCH Variation as possible"
-	if (a_str == false) { System.print("a) string failed"); return false }
-
-	// multiple matches
-	var b_str = s.upper("i") == "ThIs Is just a really long test strIng to try and get as MUCH varIatIon as possIble"
-	if (b_str == false) { System.print("b) string failed"); return false }
-
-	// 1 match, longer search string
-	var c_str = s.upper("is") == "ThIS IS just a really long test string to try and get as MUCH variation as possible"
-	if (c_str == false) { System.print("c) string failed"); return false }
-
-	// Try a character that is already uppercase, combined with a lowercase
-	var d_str = s.upper("Th") == "THis is just a really long test string to try and get as MUCH variation as possible"
-	if (d_str == false) { System.print("d) string failed"); return false }
-
-	// Try a completely uppercase string
-	var e_str = s.upper("MUC") == "This is just a really long test string to try and get as MUCH variation as possible"
-	if (e_str == false) { System.print("e) string failed"); return false }
-
-
 	// Numbers Only
 	// Numbers Only
 	// the v in variation
 	// the v in variation
 	var a_int = s.upper(62) == "This is just a really long test string to try and get as MUCH Variation as possible"
 	var a_int = s.upper(62) == "This is just a really long test string to try and get as MUCH Variation as possible"