From 09bd911babea33a3c6d394b2c1df43afa6a0f682 Mon Sep 17 00:00:00 2001 From: UnrelatedString Date: Tue, 29 Apr 2025 03:38:03 -0400 Subject: [PATCH 1/2] not even going to bother getting ci and all that set up here lmao i'm just aaaa --- src/Data/String/Common.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Data/String/Common.lua b/src/Data/String/Common.lua index 0e7b606..0dd5659 100644 --- a/src/Data/String/Common.lua +++ b/src/Data/String/Common.lua @@ -15,7 +15,13 @@ return { split = (function(sep) return function(s) local t = {} - for str in s:gmatch("([^" .. sep .. "]+)") do table.insert(t, str) end + local pattern + if string.len(s) == 0 then + pattern = "(.)" + else + pattern = "([^" .. sep .. "]+)" + end + for str in s:gmatch(pattern) do table.insert(t, str) end return t end end), From 13f1dc9f05e6904b93573763bdeded80200aa3c5 Mon Sep 17 00:00:00 2001 From: UnrelatedString Date: Tue, 29 Apr 2025 05:33:12 -0400 Subject: [PATCH 2/2] yeah no that implemenmtntion is bullhit aneyways anls o i really what the fuck happened ot me ly shechulde alike what the fuck it's 5'33 am shtiu --- .eslintrc.json | 26 -------- package.json | 18 ------ src/Data/String/CodePoints.js | 107 ------------------------------- src/Data/String/CodeUnits.js | 116 ---------------------------------- src/Data/String/Common.js | 52 --------------- src/Data/String/Common.lua | 20 +++--- src/Data/String/Common.purs | 19 +++++- src/Data/String/Unsafe.js | 11 ---- 8 files changed, 27 insertions(+), 342 deletions(-) delete mode 100644 .eslintrc.json delete mode 100644 package.json delete mode 100644 src/Data/String/CodePoints.js delete mode 100644 src/Data/String/CodeUnits.js delete mode 100644 src/Data/String/Common.js delete mode 100644 src/Data/String/Unsafe.js diff --git a/.eslintrc.json b/.eslintrc.json deleted file mode 100644 index 1c6afb9..0000000 --- a/.eslintrc.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "parserOptions": { - "ecmaVersion": 6, - "sourceType": "module" - }, - "extends": "eslint:recommended", - "rules": { - "strict": [2, "global"], - "block-scoped-var": 2, - "consistent-return": 2, - "eqeqeq": [2, "smart"], - "guard-for-in": 2, - "no-caller": 2, - "no-extend-native": 2, - "no-loop-func": 2, - "no-new": 2, - "no-param-reassign": 2, - "no-return-assign": 2, - "no-unused-expressions": 2, - "no-use-before-define": 2, - "radix": [2, "always"], - "indent": [2, 2], - "quotes": [2, "double"], - "semi": [2, "always"] - } -} diff --git a/package.json b/package.json deleted file mode 100644 index cffd45e..0000000 --- a/package.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "private": true, - "scripts": { - "clean": "rimraf output && rimraf .pulp-cache", - "build": "eslint src && pulp build -- --censor-lib --strict", - "test": "pulp test && npm run test:run:without_codePointAt", - "test:run:without_codePointAt": "node -e \"delete String.prototype.codePointAt; import('./output/Test.Main/index.js').then(m => m.main());\"", - "bench:build": "purs compile 'bench/**/*.purs' 'src/**/*.purs' 'bower_components/*/src/**/*.purs'", - "bench:run": "node --expose-gc -e 'require(\"./output/Bench.Main/index.js\").main()'", - "bench": "npm run bench:build && npm run bench:run" - }, - "devDependencies": { - "eslint": "^7.15.0", - "pulp": "16.0.0-0", - "purescript-psa": "^0.8.2", - "rimraf": "^3.0.2" - } -} diff --git a/src/Data/String/CodePoints.js b/src/Data/String/CodePoints.js deleted file mode 100644 index ebd9e39..0000000 --- a/src/Data/String/CodePoints.js +++ /dev/null @@ -1,107 +0,0 @@ -/* global Symbol */ - -var hasArrayFrom = typeof Array.from === "function"; -var hasStringIterator = - typeof Symbol !== "undefined" && - Symbol != null && - typeof Symbol.iterator !== "undefined" && - typeof String.prototype[Symbol.iterator] === "function"; -var hasFromCodePoint = typeof String.prototype.fromCodePoint === "function"; -var hasCodePointAt = typeof String.prototype.codePointAt === "function"; - -export const _unsafeCodePointAt0 = function (fallback) { - return hasCodePointAt - ? function (str) { return str.codePointAt(0); } - : fallback; -}; - -export const _codePointAt = function (fallback) { - return function (Just) { - return function (Nothing) { - return function (unsafeCodePointAt0) { - return function (index) { - return function (str) { - var length = str.length; - if (index < 0 || index >= length) return Nothing; - if (hasStringIterator) { - var iter = str[Symbol.iterator](); - for (var i = index;; --i) { - var o = iter.next(); - if (o.done) return Nothing; - if (i === 0) return Just(unsafeCodePointAt0(o.value)); - } - } - return fallback(index)(str); - }; - }; - }; - }; - }; -}; - -export const _countPrefix = function (fallback) { - return function (unsafeCodePointAt0) { - if (hasStringIterator) { - return function (pred) { - return function (str) { - var iter = str[Symbol.iterator](); - for (var cpCount = 0; ; ++cpCount) { - var o = iter.next(); - if (o.done) return cpCount; - var cp = unsafeCodePointAt0(o.value); - if (!pred(cp)) return cpCount; - } - }; - }; - } - return fallback; - }; -}; - -export const _fromCodePointArray = function (singleton) { - return hasFromCodePoint - ? function (cps) { - // Function.prototype.apply will fail for very large second parameters, - // so we don't use it for arrays with 10,000 or more entries. - if (cps.length < 10e3) { - return String.fromCodePoint.apply(String, cps); - } - return cps.map(singleton).join(""); - } - : function (cps) { - return cps.map(singleton).join(""); - }; -}; - -export const _singleton = function (fallback) { - return hasFromCodePoint ? String.fromCodePoint : fallback; -}; - -export const _take = function (fallback) { - return function (n) { - if (hasStringIterator) { - return function (str) { - var accum = ""; - var iter = str[Symbol.iterator](); - for (var i = 0; i < n; ++i) { - var o = iter.next(); - if (o.done) return accum; - accum += o.value; - } - return accum; - }; - } - return fallback(n); - }; -}; - -export const _toCodePointArray = function (fallback) { - return function (unsafeCodePointAt0) { - if (hasArrayFrom) { - return function (str) { - return Array.from(str, unsafeCodePointAt0); - }; - } - return fallback; - }; -}; diff --git a/src/Data/String/CodeUnits.js b/src/Data/String/CodeUnits.js deleted file mode 100644 index 2608384..0000000 --- a/src/Data/String/CodeUnits.js +++ /dev/null @@ -1,116 +0,0 @@ -export const fromCharArray = function (a) { - return a.join(""); -}; - -export const toCharArray = function (s) { - return s.split(""); -}; - -export const singleton = function (c) { - return c; -}; - -export const _charAt = function (just) { - return function (nothing) { - return function (i) { - return function (s) { - return i >= 0 && i < s.length ? just(s.charAt(i)) : nothing; - }; - }; - }; -}; - -export const _toChar = function (just) { - return function (nothing) { - return function (s) { - return s.length === 1 ? just(s) : nothing; - }; - }; -}; - -export const length = function (s) { - return s.length; -}; - -export const countPrefix = function (p) { - return function (s) { - var i = 0; - while (i < s.length && p(s.charAt(i))) i++; - return i; - }; -}; - -export const _indexOf = function (just) { - return function (nothing) { - return function (x) { - return function (s) { - var i = s.indexOf(x); - return i === -1 ? nothing : just(i); - }; - }; - }; -}; - -export const _indexOfStartingAt = function (just) { - return function (nothing) { - return function (x) { - return function (startAt) { - return function (s) { - if (startAt < 0 || startAt > s.length) return nothing; - var i = s.indexOf(x, startAt); - return i === -1 ? nothing : just(i); - }; - }; - }; - }; -}; - -export const _lastIndexOf = function (just) { - return function (nothing) { - return function (x) { - return function (s) { - var i = s.lastIndexOf(x); - return i === -1 ? nothing : just(i); - }; - }; - }; -}; - -export const _lastIndexOfStartingAt = function (just) { - return function (nothing) { - return function (x) { - return function (startAt) { - return function (s) { - var i = s.lastIndexOf(x, startAt); - return i === -1 ? nothing : just(i); - }; - }; - }; - }; -}; - -export const take = function (n) { - return function (s) { - return s.substr(0, n); - }; -}; - -export const drop = function (n) { - return function (s) { - return s.substring(n); - }; -}; - -export const slice = function (b) { - return function (e) { - return function (s) { - return s.slice(b,e); - }; - }; -}; - -export const splitAt = function (i) { - return function (s) { - return { before: s.substring(0, i), after: s.substring(i) }; - }; -}; diff --git a/src/Data/String/Common.js b/src/Data/String/Common.js deleted file mode 100644 index 5693585..0000000 --- a/src/Data/String/Common.js +++ /dev/null @@ -1,52 +0,0 @@ -export const _localeCompare = function (lt) { - return function (eq) { - return function (gt) { - return function (s1) { - return function (s2) { - var result = s1.localeCompare(s2); - return result < 0 ? lt : result > 0 ? gt : eq; - }; - }; - }; - }; -}; - -export const replace = function (s1) { - return function (s2) { - return function (s3) { - return s3.replace(s1, s2); - }; - }; -}; - -export const replaceAll = function (s1) { - return function (s2) { - return function (s3) { - return s3.replace(new RegExp(s1.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"), "g"), s2); // eslint-disable-line no-useless-escape - }; - }; -}; - -export const split = function (sep) { - return function (s) { - return s.split(sep); - }; -}; - -export const toLower = function (s) { - return s.toLowerCase(); -}; - -export const toUpper = function (s) { - return s.toUpperCase(); -}; - -export const trim = function (s) { - return s.trim(); -}; - -export const joinWith = function (s) { - return function (xs) { - return xs.join(s); - }; -}; diff --git a/src/Data/String/Common.lua b/src/Data/String/Common.lua index 0dd5659..6c7745f 100644 --- a/src/Data/String/Common.lua +++ b/src/Data/String/Common.lua @@ -12,19 +12,17 @@ return { replaceAll = (function(pattern) return function(replacement) return function(string) return string:gsub(pattern, replacement) end end end), - split = (function(sep) - return function(s) - local t = {} - local pattern - if string.len(s) == 0 then - pattern = "(.)" - else - pattern = "([^" .. sep .. "]+)" - end - for str in s:gmatch(pattern) do table.insert(t, str) end - return t + findSubstringFrom = (function(pattern, in, from, just, nothing) + local start = string.find(in, pattern, from, true) + if start == nil then + return nothing + else + return just(start) end end), + grabSubstringIGuess = (function(s, i, j) + return string.sub(s, i, j) + end), toLower = (function(s) return s:lower() end), toUpper = (function(s) return s:upper() end), trim = (function(s) return s:match("^%s*(.-)%s*$") end), diff --git a/src/Data/String/Common.purs b/src/Data/String/Common.purs index 9e3132e..4530c37 100644 --- a/src/Data/String/Common.purs +++ b/src/Data/String/Common.purs @@ -12,7 +12,11 @@ module Data.String.Common import Prelude +import Control.Monad.ST (ST) import Data.String.Pattern (Pattern, Replacement) +import Data.Function.Uncurried (Fn5, runFn5) +import Data.Array.ST as STAr +import Data.Maybe (Maybe(..)) -- | Returns `true` if the given string is empty. -- | @@ -56,13 +60,26 @@ foreign import replace :: Pattern -> Replacement -> String -> String -- | ``` foreign import replaceAll :: Pattern -> Replacement -> String -> String +foreign import findSubstringFrom :: Fn5 Pattern String Int (Int -> Maybe Int) (Maybe Int) Maybe Int + +foreign import grabSubstringIGuess :: Fn3 String Int Int String + -- | Returns the substrings of the second string separated along occurences -- | of the first string. -- | -- | ```purescript -- | split (Pattern " ") "hello world" == ["hello", "world"] -- | ``` -foreign import split :: Pattern -> String -> Array String +split :: Pattern -> String -> Array String +split sep str = STAr.run $ split' STAr.new 1 + where split' :: forall h. STAr.STArray h String -> Int -> ST h (STAr.STArray h String) + split' a i + | Just at <- runFn5 findSubstringFrom sep str i Just Nothing = + do + let + STAr.push chunk a + split' a i' + | otherwise = a -- | Returns the argument converted to lowercase. -- | diff --git a/src/Data/String/Unsafe.js b/src/Data/String/Unsafe.js deleted file mode 100644 index 75772aa..0000000 --- a/src/Data/String/Unsafe.js +++ /dev/null @@ -1,11 +0,0 @@ -export const charAt = function (i) { - return function (s) { - if (i >= 0 && i < s.length) return s.charAt(i); - throw new Error("Data.String.Unsafe.charAt: Invalid index."); - }; -}; - -export const char = function (s) { - if (s.length === 1) return s.charAt(0); - throw new Error("Data.String.Unsafe.char: Expected string of length 1."); -};