From b608bb63b0fefabd748dc8e9d119618f5df9c9a2 Mon Sep 17 00:00:00 2001 From: Xiang Date: Sat, 4 Apr 2020 17:54:08 +0800 Subject: [PATCH 01/14] init --- Data/String/CodePoints.py | 55 +++++++++++++++++++++++ Data/String/CodeUnits.py | 88 ++++++++++++++++++++++++++++++++++++ Data/String/Common.py | 45 +++++++++++++++++++ Data/String/Regex.py | 93 +++++++++++++++++++++++++++++++++++++++ Data/String/Unsafe.py | 14 ++++++ README.md | 7 ++- 6 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 Data/String/CodePoints.py create mode 100644 Data/String/CodeUnits.py create mode 100644 Data/String/Common.py create mode 100644 Data/String/Regex.py create mode 100644 Data/String/Unsafe.py diff --git a/Data/String/CodePoints.py b/Data/String/CodePoints.py new file mode 100644 index 0000000..9e5fce9 --- /dev/null +++ b/Data/String/CodePoints.py @@ -0,0 +1,55 @@ +def _unsafeCodePointAt0(fallback): + def ap(s): + return ord(s[0]) + + return ap + + +def _codePointAt(fallback, just, nothing, unsafeCodePointAt0, index, s): + length = len(s) + if index < 0 or index >= length: + return nothing + else: + return just(unsafeCodePointAt0(s[index])) + + +def _codePointAt(fallback): + return lambda just: lambda nothing: lambda unsafeCodePointAt0: lambda index: lambda s: _codePointAtImpl( + fallback, just, nothing, unsafeCodePointAt0, index, s + ) + + +def _codePrefixImpl(fallback, unsafeCodePointAt0, pred, s): + cpCount = 0 + for x in s: + cp = unsafeCodePointAt0(x) + if not pred(cp): + return cpCount + else: + cpCount += 1 + return cpCount + + +def _fromCodePointArray(singleton): + def ap(cps): + return "".join(map(_singleton, cps)) + + return ap + + +def _singleton(fallback): + return chr + + +def _take(fallback): + def ap(n): + return lambda s: s[:n] + + return ap + + +def _toCodePointArray(fallback): + def ap(unsafeCodePointAt0): + return lambda s: list(map(unsafeCodePointAt0, s)) + + return ap diff --git a/Data/String/CodeUnits.py b/Data/String/CodeUnits.py new file mode 100644 index 0000000..4d653bb --- /dev/null +++ b/Data/String/CodeUnits.py @@ -0,0 +1,88 @@ +def fromCharArray(a): + return "".join(a) + + +def toCharArray(s): + return s.split("") + + +def singleton(c): + return c + + +def _charAt(just): + return ( + lambda nothing: lambda i: lambda s: just(s[i]) + if i >= 0 and i < len(s) + else nothing + ) + + +def _toChar(just): + return lambda nothing: lambda s: just(s) if len(s) == 1 else nothing + + +length = len + + +def countPrefix(p): + def ap(s): + i = 0 + while i < len(s) and p(s[i]): + i += 1 + return i + + return ap + + +def _indexOfImpl(just, nothing, x, s): + i = s.find(x) + return nothing if i == -1 else just(i) + + +def _indexOf(just): + return lambda nothing: lambda x: lambda s: _indexOfImpl(just, nothing, x, s) + + +def _indexOfStartingAtImpl(just, nothing, x, startAt, s): + if startAt < 0 or startAt > len(s): + return nothing + i = s.find(x, startAt) + return nothing if i == -1 else just(i) + + +def _lastIndexOfImpl(just, nothing, x, s: str): + i = s.rfind(x) + return nothing if i == -1 else just(i) + + +def _lastIndexOf(just): + return lambda nothing: lambda x: lambda s: _lastIndexOfImpl(just, nothing, x, s) + + +def _lastIndexOfStartAtImpl(just, nothing, x, startAt, s): + if startAt < 0 or startAt > len(s): + return nothing + i = s.rfind(x, startAt) + return nothing if i == -1 else just(i) + + +def _lastIndexOfStartingAt(just): + return lambda nothing: lambda x: lambda startAt: lambda s: _lastIndexOfStartAtImpl( + just, nothing, x, startAt, s + ) + + +def take(n: int): + return lambda s: s[:n] + +def drop(n: int): + return lambda s: [n:] + + +def _slice(b): + return lambda e: lambda s: s[b:e] + + +def splitAt(i): + return lambda s: {"before": s[:i], "after": s[i:]} diff --git a/Data/String/Common.py b/Data/String/Common.py new file mode 100644 index 0000000..7b1430e --- /dev/null +++ b/Data/String/Common.py @@ -0,0 +1,45 @@ +def _localeCompareImpl(lt, eq, gt, s1, s2): + # TODO: write correct localeCompare + + # var result = s1.localeCompare(s2); + # return result < 0 ? lt : result > 0 ? gt : eq; + if s1 == s2: + return eq + if s1 < s2: + return lt + if s1 > s2: + return gt + + +def _localeCompare(lt): + return lambda eq: lambda gt: lambda s1: lambda s2: _localeCompareImpl( + lt, eq, gt, s1, s2 + ) + + +def replace(s1): + return lambda s2: lambda s3: s3.replace(s1, s2, 1) + + +def replaceAll(s1): + return lambda s2: lambda s3: s3.replace(s1, s2) + + +def split(sep): + return lambda s: s.split(sep) + + +def toLower(s): + return s.lower() + + +def toUpper(s): + return s.upper() + + +def trim(s): + return s.strip() + + +def joinWith(s): + return lambda xs: s.join(xs) diff --git a/Data/String/Regex.py b/Data/String/Regex.py new file mode 100644 index 0000000..d4ba55b --- /dev/null +++ b/Data/String/Regex.py @@ -0,0 +1,93 @@ +import re + + +def showRegexImpl(r): + return "" + r + + +def regexImpl(left): + raise NotImplementedError("FFI Not implemented. Data.String.Regex.regexImpl") + + +# return function (right) { +# return function (s1) { +# return function (s2) { +# try { +# return right(new RegExp(s1, s2)); +# } catch (e) { +# return left(e.message); +# } + + +def source(r): + return r["source"] + + +def flagsImpl(r): + return { + "multiline": r["multiline"], + "ignoreCase": r["ignoreCase"], + "global": r["global"], + "sticky": bool(r["sticky"]), + "unicode": bool(r["unicode"]), + } + + +def test(r): + raise NotImplementedError("FFI Not implemented. Data.String.Regex.test") + + +# return function (s) { +# var lastIndex = r.lastIndex; +# var result = r.test(s); +# r.lastIndex = lastIndex; +# return result; + + +def _match(just): + raise NotImplementedError("FFI Not implemented. Data.String.Regex._match") + + +# return function (nothing) { +# return function (r) { +# return function (s) { +# var m = s.match(r); +# if (m == null || m.length === 0) { +# return nothing; +# } else { +# for (var i = 0; i < m.length; i++) { +# m[i] = m[i] == null ? nothing : just(m[i]); +# } +# return just(m); +# } + + +def replace(r): + return lambda s1: lambda s2: s2.replace(s1, 1) + + +def replaceBy(r): + raise NotImplementedError("FFI Not implemented. Data.String.Regex.replaceBy") + + +# return function (f) { +# return function (s2) { +# return s2.replace(r, function (match) { +# return f(match)(Array.prototype.splice.call(arguments, 1, arguments.length - 3)); +# }); + + +def _searchImpl(just, nothing, r, s): + m = re.search(r, s) + if m: + return just(m.start()) + else: + return nothing + + +def _search(just): + return lambda nothing: lambda r: lambda s: _searchImpl(just, nothing, r, s) + + +def split(r): + return lambda s: s.split(r) diff --git a/Data/String/Unsafe.py b/Data/String/Unsafe.py new file mode 100644 index 0000000..826a8a0 --- /dev/null +++ b/Data/String/Unsafe.py @@ -0,0 +1,14 @@ +def charAt(i): + def ap(s): + if i > 0 and i < len(s): + return s[i] + + raise IndexError("Data.String.Unsafe.charAt: Invalid index.") + + return ap + + +def char(s): + if len(s) == 1: + return s[0] + raise ValueError("Data.String.Unsafe.char: Expected string of length 1.") diff --git a/README.md b/README.md index fc2fe65..4dcaf68 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# strings \ No newline at end of file +# A Python FFI provider for purescript-strings +Providing FFI files for https://github.com/purescript/purescript-strings + +Available mirrors: + +https://github.com/purescript-python/purescript-python-ffi-index From 7fc4cfb4ec13b896f6ca106e35d9c0ceb22740f5 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 13:24:20 +0800 Subject: [PATCH 02/14] move files --- {Data => python-ffi/Data}/String/CodePoints.py | 0 {Data => python-ffi/Data}/String/CodeUnits.py | 0 {Data => python-ffi/Data}/String/Common.py | 0 {Data => python-ffi/Data}/String/Regex.py | 0 {Data => python-ffi/Data}/String/Unsafe.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename {Data => python-ffi/Data}/String/CodePoints.py (100%) rename {Data => python-ffi/Data}/String/CodeUnits.py (100%) rename {Data => python-ffi/Data}/String/Common.py (100%) rename {Data => python-ffi/Data}/String/Regex.py (100%) rename {Data => python-ffi/Data}/String/Unsafe.py (100%) diff --git a/Data/String/CodePoints.py b/python-ffi/Data/String/CodePoints.py similarity index 100% rename from Data/String/CodePoints.py rename to python-ffi/Data/String/CodePoints.py diff --git a/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py similarity index 100% rename from Data/String/CodeUnits.py rename to python-ffi/Data/String/CodeUnits.py diff --git a/Data/String/Common.py b/python-ffi/Data/String/Common.py similarity index 100% rename from Data/String/Common.py rename to python-ffi/Data/String/Common.py diff --git a/Data/String/Regex.py b/python-ffi/Data/String/Regex.py similarity index 100% rename from Data/String/Regex.py rename to python-ffi/Data/String/Regex.py diff --git a/Data/String/Unsafe.py b/python-ffi/Data/String/Unsafe.py similarity index 100% rename from Data/String/Unsafe.py rename to python-ffi/Data/String/Unsafe.py From 743e0619a9a369e7c4b021a89fc6f39681d0c878 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 18:16:05 +0800 Subject: [PATCH 03/14] fix: typo --- python-ffi/Data/String/CodePoints.py | 2 +- python-ffi/Data/String/CodeUnits.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python-ffi/Data/String/CodePoints.py b/python-ffi/Data/String/CodePoints.py index 9e5fce9..85c8f80 100644 --- a/python-ffi/Data/String/CodePoints.py +++ b/python-ffi/Data/String/CodePoints.py @@ -5,7 +5,7 @@ def ap(s): return ap -def _codePointAt(fallback, just, nothing, unsafeCodePointAt0, index, s): +def _codePointAtImpl(fallback, just, nothing, unsafeCodePointAt0, index, s): length = len(s) if index < 0 or index >= length: return nothing diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index 4d653bb..f052fb6 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -77,7 +77,7 @@ def take(n: int): return lambda s: s[:n] def drop(n: int): - return lambda s: [n:] + return lambda s: s[n:] def _slice(b): From 9285b444e80e3cfa5a3d55702eefd37c1e0ff37b Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 18:35:45 +0800 Subject: [PATCH 04/14] fix: sync to 4.0.1 --- python-ffi/Data/String/CodeUnits.py | 43 +++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index f052fb6..3a0015f 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -11,11 +11,19 @@ def singleton(c): def _charAt(just): - return ( - lambda nothing: lambda i: lambda s: just(s[i]) - if i >= 0 and i < len(s) - else nothing - ) + def nothing_(nothing): + def i_(i): + def s_(s): + if i >= 0 and i < len(s): + return just(s[i]) + else: + return nothing + + return s_ + + return i_ + + return nothing_ def _toChar(just): @@ -44,6 +52,11 @@ def _indexOf(just): return lambda nothing: lambda x: lambda s: _indexOfImpl(just, nothing, x, s) +def _indexOfPImpl(just, nothing, x, startAt, s: str): + i = s.find(x, startAt) + return nothing if i == -1 else just(i) + + def _indexOfStartingAtImpl(just, nothing, x, startAt, s): if startAt < 0 or startAt > len(s): return nothing @@ -51,11 +64,23 @@ def _indexOfStartingAtImpl(just, nothing, x, startAt, s): return nothing if i == -1 else just(i) +globals()[ + "_indexOf'" +] = lambda just: lambda nothing: lambda startAt: lambda x: lambda s: _indexOfStartingAtImpl( + just, nothing, startAt, x, s +) + + def _lastIndexOfImpl(just, nothing, x, s: str): i = s.rfind(x) return nothing if i == -1 else just(i) +def _lastIndexOfPImpl(just, nothing, x, startAt, s: str): + i = s.rfind(x, startAt) + return nothing if i == -1 else just(i) + + def _lastIndexOf(just): return lambda nothing: lambda x: lambda s: _lastIndexOfImpl(just, nothing, x, s) @@ -67,6 +92,13 @@ def _lastIndexOfStartAtImpl(just, nothing, x, startAt, s): return nothing if i == -1 else just(i) +globals()[ + "_lastIndexOf'" +] = lambda just: lambda nothing: lambda startAt: lambda x: lambda s: _lastIndexOfStartAtImpl( + just, nothing, x, startAt, s +) + + def _lastIndexOfStartingAt(just): return lambda nothing: lambda x: lambda startAt: lambda s: _lastIndexOfStartAtImpl( just, nothing, x, startAt, s @@ -76,6 +108,7 @@ def _lastIndexOfStartingAt(just): def take(n: int): return lambda s: s[:n] + def drop(n: int): return lambda s: s[n:] From f2da8ddd5182d8881b2a230732c088ca2b4f6e59 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 19:34:19 +0800 Subject: [PATCH 05/14] add _countPrefix --- python-ffi/Data/String/CodePoints.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/python-ffi/Data/String/CodePoints.py b/python-ffi/Data/String/CodePoints.py index 85c8f80..3148580 100644 --- a/python-ffi/Data/String/CodePoints.py +++ b/python-ffi/Data/String/CodePoints.py @@ -53,3 +53,21 @@ def ap(unsafeCodePointAt0): return lambda s: list(map(unsafeCodePointAt0, s)) return ap + + +def _countPrefix(fallback): + def unsafeCodePointAt0_(unsafeCodePointAt0): + def pred_(pred): + def str_(s): + count = 0 + for x in s: + if not pred(x): + return count + else: + count += 1 + + return str_ + + return pred_ + + return unsafeCodePointAt0_ From 0c7f090a2deba778064f8d68605dabdf0f845bd5 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 19:38:42 +0800 Subject: [PATCH 06/14] add 4.0.1 compact names --- python-ffi/Data/String/Regex.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python-ffi/Data/String/Regex.py b/python-ffi/Data/String/Regex.py index d4ba55b..26d19cc 100644 --- a/python-ffi/Data/String/Regex.py +++ b/python-ffi/Data/String/Regex.py @@ -4,6 +4,8 @@ def showRegexImpl(r): return "" + r +globals()['showRegex'] = showRegexImpl + def regexImpl(left): raise NotImplementedError("FFI Not implemented. Data.String.Regex.regexImpl") @@ -18,6 +20,7 @@ def regexImpl(left): # return left(e.message); # } +globals()['regexImpl'] = regexImpl def source(r): return r["source"] @@ -33,6 +36,8 @@ def flagsImpl(r): } +globals()['flags'] = regexImpl + def test(r): raise NotImplementedError("FFI Not implemented. Data.String.Regex.test") @@ -69,6 +74,7 @@ def replace(r): def replaceBy(r): raise NotImplementedError("FFI Not implemented. Data.String.Regex.replaceBy") +globals()['replace'] = replaceBy # return function (f) { # return function (s2) { From ac54735c8fa6ef57c92c5241f81ab8b0836a7db7 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 19:41:35 +0800 Subject: [PATCH 07/14] fix: function names --- python-ffi/Data/String/Regex.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python-ffi/Data/String/Regex.py b/python-ffi/Data/String/Regex.py index 26d19cc..7b93f69 100644 --- a/python-ffi/Data/String/Regex.py +++ b/python-ffi/Data/String/Regex.py @@ -4,7 +4,7 @@ def showRegexImpl(r): return "" + r -globals()['showRegex'] = showRegexImpl +globals()["showRegex'"] = showRegexImpl def regexImpl(left): @@ -20,7 +20,7 @@ def regexImpl(left): # return left(e.message); # } -globals()['regexImpl'] = regexImpl +globals()["regex'"] = regexImpl def source(r): return r["source"] @@ -36,7 +36,7 @@ def flagsImpl(r): } -globals()['flags'] = regexImpl +globals()["flags'"] = regexImpl def test(r): raise NotImplementedError("FFI Not implemented. Data.String.Regex.test") @@ -74,7 +74,7 @@ def replace(r): def replaceBy(r): raise NotImplementedError("FFI Not implemented. Data.String.Regex.replaceBy") -globals()['replace'] = replaceBy +globals()["replace'"] = replaceBy # return function (f) { # return function (s2) { From d1d3d9abf8817bf484c8d1e64846c3f1e3d9dc76 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 19:54:59 +0800 Subject: [PATCH 08/14] fix split impl --- python-ffi/Data/String/Common.py | 5 ++++- python-ffi/Data/String/Regex.py | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/python-ffi/Data/String/Common.py b/python-ffi/Data/String/Common.py index 7b1430e..d760078 100644 --- a/python-ffi/Data/String/Common.py +++ b/python-ffi/Data/String/Common.py @@ -26,7 +26,10 @@ def replaceAll(s1): def split(sep): - return lambda s: s.split(sep) + if len(sep) == 0: + return lambda s: list(s) + else: + return lambda s: s.split(sep) def toLower(s): diff --git a/python-ffi/Data/String/Regex.py b/python-ffi/Data/String/Regex.py index 7b93f69..4646046 100644 --- a/python-ffi/Data/String/Regex.py +++ b/python-ffi/Data/String/Regex.py @@ -4,6 +4,7 @@ def showRegexImpl(r): return "" + r + globals()["showRegex'"] = showRegexImpl @@ -22,6 +23,7 @@ def regexImpl(left): globals()["regex'"] = regexImpl + def source(r): return r["source"] @@ -38,6 +40,7 @@ def flagsImpl(r): globals()["flags'"] = regexImpl + def test(r): raise NotImplementedError("FFI Not implemented. Data.String.Regex.test") @@ -49,8 +52,15 @@ def test(r): # return result; -def _match(just): - raise NotImplementedError("FFI Not implemented. Data.String.Regex._match") +def _matchImpl(just, nothing, r, s): + m = re.match(r, s) + if m is None: + return nothing + else: + return just(m) + + +_match = lambda j: lambda n: lambda r: lambda s: _matchImpl(j, n, r, s) # return function (nothing) { @@ -74,6 +84,7 @@ def replace(r): def replaceBy(r): raise NotImplementedError("FFI Not implemented. Data.String.Regex.replaceBy") + globals()["replace'"] = replaceBy # return function (f) { From 8f8aaba8180db338919f8cfa020c2b1a3f264947 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 21:29:41 +0800 Subject: [PATCH 09/14] fix charAt index range --- python-ffi/Data/String/Unsafe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-ffi/Data/String/Unsafe.py b/python-ffi/Data/String/Unsafe.py index 826a8a0..455d7e8 100644 --- a/python-ffi/Data/String/Unsafe.py +++ b/python-ffi/Data/String/Unsafe.py @@ -1,6 +1,6 @@ def charAt(i): def ap(s): - if i > 0 and i < len(s): + if i >= 0 and i < len(s): return s[i] raise IndexError("Data.String.Unsafe.charAt: Invalid index.") From e334f79587c3e6997251ec628ea67dd23c572f79 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 21:33:50 +0800 Subject: [PATCH 10/14] fix argument order of _indexOf' --- python-ffi/Data/String/CodeUnits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index 3a0015f..d84d449 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -66,7 +66,7 @@ def _indexOfStartingAtImpl(just, nothing, x, startAt, s): globals()[ "_indexOf'" -] = lambda just: lambda nothing: lambda startAt: lambda x: lambda s: _indexOfStartingAtImpl( +] = lambda just: lambda nothing: lambda x: lambda startAt: lambda s: _indexOfStartingAtImpl( just, nothing, startAt, x, s ) From 337ad283fabb86e0f7a23ecdadfa7fd3502bb8bf Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 21:35:16 +0800 Subject: [PATCH 11/14] fix argument order --- python-ffi/Data/String/CodeUnits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index d84d449..8bfd634 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -67,7 +67,7 @@ def _indexOfStartingAtImpl(just, nothing, x, startAt, s): globals()[ "_indexOf'" ] = lambda just: lambda nothing: lambda x: lambda startAt: lambda s: _indexOfStartingAtImpl( - just, nothing, startAt, x, s + just, nothing, x, startAt, s ) From de388a052ac136a97204edd82ed39b8a32df1740 Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 22:23:33 +0800 Subject: [PATCH 12/14] fix: argument orders --- python-ffi/Data/String/CodeUnits.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index 8bfd634..d4d81fd 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -58,6 +58,7 @@ def _indexOfPImpl(just, nothing, x, startAt, s: str): def _indexOfStartingAtImpl(just, nothing, x, startAt, s): + print(x, startAt, s) if startAt < 0 or startAt > len(s): return nothing i = s.find(x, startAt) @@ -94,7 +95,7 @@ def _lastIndexOfStartAtImpl(just, nothing, x, startAt, s): globals()[ "_lastIndexOf'" -] = lambda just: lambda nothing: lambda startAt: lambda x: lambda s: _lastIndexOfStartAtImpl( +] = lambda just: lambda nothing: lambda x: lambda startAt: lambda s: _lastIndexOfStartAtImpl( just, nothing, x, startAt, s ) From f09a83836034813ff9c6b39c6d3ce4887df20d9b Mon Sep 17 00:00:00 2001 From: Xiang Date: Tue, 9 Jun 2020 22:23:56 +0800 Subject: [PATCH 13/14] clear --- python-ffi/Data/String/CodeUnits.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index d4d81fd..155b0c8 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -58,7 +58,6 @@ def _indexOfPImpl(just, nothing, x, startAt, s: str): def _indexOfStartingAtImpl(just, nothing, x, startAt, s): - print(x, startAt, s) if startAt < 0 or startAt > len(s): return nothing i = s.find(x, startAt) From 623dbb85fcf9021ef2da7face9bd1853128f58ca Mon Sep 17 00:00:00 2001 From: Xiang Date: Wed, 10 Jun 2020 09:09:25 +0800 Subject: [PATCH 14/14] fix code unit tests bugs --- python-ffi/Data/String/CodeUnits.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python-ffi/Data/String/CodeUnits.py b/python-ffi/Data/String/CodeUnits.py index 155b0c8..328c749 100644 --- a/python-ffi/Data/String/CodeUnits.py +++ b/python-ffi/Data/String/CodeUnits.py @@ -3,7 +3,7 @@ def fromCharArray(a): def toCharArray(s): - return s.split("") + return list(s) def singleton(c): @@ -88,7 +88,7 @@ def _lastIndexOf(just): def _lastIndexOfStartAtImpl(just, nothing, x, startAt, s): if startAt < 0 or startAt > len(s): return nothing - i = s.rfind(x, startAt) + i = s.rfind(x, 0, startAt + len(x)) return nothing if i == -1 else just(i) @@ -106,11 +106,11 @@ def _lastIndexOfStartingAt(just): def take(n: int): - return lambda s: s[:n] + return lambda s: s[: max(n, 0)] def drop(n: int): - return lambda s: s[n:] + return lambda s: s[max(n, 0) :] def _slice(b): @@ -118,4 +118,4 @@ def _slice(b): def splitAt(i): - return lambda s: {"before": s[:i], "after": s[i:]} + return lambda s: {"before": s[: max(i, 0)], "after": s[max(i, 0) :]}