Browse Source

Correct _split_iterator

gingerBill 3 years ago
parent
commit
f561147190
2 changed files with 20 additions and 79 deletions
  1. 8 32
      core/bytes/bytes.odin
  2. 12 47
      core/strings/strings.odin

+ 8 - 32
core/bytes/bytes.odin

@@ -219,39 +219,24 @@ split_after_n :: proc(s, sep: []byte, n: int, allocator := context.allocator) ->
 
 
 @private
 @private
 _split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save, n: int) -> (res: []byte, ok: bool) {
 _split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save, n: int) -> (res: []byte, ok: bool) {
-	s, n := s, n
-
-	if n == 0 {
-		return
-	}
-
-	if sep == nil {
+	if sep == "" {
 		res = s[:]
 		res = s[:]
 		ok = true
 		ok = true
 		s^ = s[len(s):]
 		s^ = s[len(s):]
 		return
 		return
 	}
 	}
 
 
-	if n < 0 {
-		n = count(s^, sep) + 1
-	}
-
-	n -= 1
-
-	i := 0
-	for ; i < n; i += 1 {
-		m := index(s^, sep)
-		if m < 0 {
-			break
-		}
+	m := index(s^, sep)
+	if m < 0 {
+		// not found
+		res = s[:]
+		ok = res != ""
+		s^ = s[len(s):]
+	} else {
 		res = s[:m+sep_save]
 		res = s[:m+sep_save]
 		ok = true
 		ok = true
 		s^ = s[m+len(sep):]
 		s^ = s[m+len(sep):]
-		return
 	}
 	}
-	res = s[:]
-	ok = res != nil
-	s^ = s[len(s):]
 	return
 	return
 }
 }
 
 
@@ -260,19 +245,10 @@ split_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) {
 	return _split_iterator(s, sep, 0, -1)
 	return _split_iterator(s, sep, 0, -1)
 }
 }
 
 
-split_n_iterator :: proc(s: ^[]byte, sep: []byte, n: int) -> ([]byte, bool) {
-	return _split_iterator(s, sep, 0, n)
-}
-
 split_after_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) {
 split_after_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) {
 	return _split_iterator(s, sep, len(sep), -1)
 	return _split_iterator(s, sep, len(sep), -1)
 }
 }
 
 
-split_after_n_iterator :: proc(s: ^[]byte, sep: []byte, n: int) -> ([]byte, bool) {
-	return _split_iterator(s, sep, len(sep), n)
-}
-
-
 
 
 index_byte :: proc(s: []byte, c: byte) -> int {
 index_byte :: proc(s: []byte, c: byte) -> int {
 	for i := 0; i < len(s); i += 1 {
 	for i := 0; i < len(s); i += 1 {

+ 12 - 47
core/strings/strings.odin

@@ -298,13 +298,7 @@ split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator) ->
 
 
 
 
 @private
 @private
-_split_iterator :: proc(s: ^string, sep: string, sep_save, n: int) -> (res: string, ok: bool) {
-	s, n := s, n
-
-	if n == 0 {
-		return
-	}
-
+_split_iterator :: proc(s: ^string, sep: string, sep_save: int) -> (res: string, ok: bool) {
 	if sep == "" {
 	if sep == "" {
 		res = s[:]
 		res = s[:]
 		ok = true
 		ok = true
@@ -312,44 +306,27 @@ _split_iterator :: proc(s: ^string, sep: string, sep_save, n: int) -> (res: stri
 		return
 		return
 	}
 	}
 
 
-	if n < 0 {
-		n = count(s^, sep) + 1
-	}
-
-	n -= 1
-
-	i := 0
-	for ; i < n; i += 1 {
-		m := index(s^, sep)
-		if m < 0 {
-			break
-		}
+	m := index(s^, sep)
+	if m < 0 {
+		// not found
+		res = s[:]
+		ok = res != ""
+		s^ = s[len(s):]
+	} else {
 		res = s[:m+sep_save]
 		res = s[:m+sep_save]
 		ok = true
 		ok = true
 		s^ = s[m+len(sep):]
 		s^ = s[m+len(sep):]
-		return
 	}
 	}
-	res = s[:]
-	ok = res != ""
-	s^ = s[len(s):]
 	return
 	return
 }
 }
 
 
 
 
 split_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
 split_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
-	return _split_iterator(s, sep, 0, -1)
-}
-
-split_n_iterator :: proc(s: ^string, sep: string, n: int) -> (string, bool) {
-	return _split_iterator(s, sep, 0, n)
+	return _split_iterator(s, sep, 0)
 }
 }
 
 
 split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
 split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
-	return _split_iterator(s, sep, len(sep), -1)
-}
-
-split_after_n_iterator :: proc(s: ^string, sep: string, n: int) -> (string, bool) {
-	return _split_iterator(s, sep, len(sep), n)
+	return _split_iterator(s, sep, len(sep))
 }
 }
 
 
 
 
@@ -402,25 +379,13 @@ split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator) -
 
 
 split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
 split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
 	sep :: "\n"
 	sep :: "\n"
-	line = _split_iterator(s, sep, 0, -1) or_return
-	return _trim_cr(line), true
-}
-
-split_lines_n_iterator :: proc(s: ^string, n: int) -> (line: string, ok: bool) {
-	sep :: "\n"
-	line = _split_iterator(s, sep, 0, n) or_return
+	line = _split_iterator(s, sep, 0) or_return
 	return _trim_cr(line), true
 	return _trim_cr(line), true
 }
 }
 
 
 split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
 split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
 	sep :: "\n"
 	sep :: "\n"
-	line = _split_iterator(s, sep, len(sep), -1) or_return
-	return _trim_cr(line), true
-}
-
-split_lines_after_n_iterator :: proc(s: ^string, n: int) -> (line: string, ok: bool) {
-	sep :: "\n"
-	line = _split_iterator(s, sep, len(sep), n) or_return
+	line = _split_iterator(s, sep, len(sep)) or_return
 	return _trim_cr(line), true
 	return _trim_cr(line), true
 }
 }