Skip to content

Commit

Permalink
s/pushChar/prependChar/g
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Apr 14, 2024
1 parent 3f29439 commit 3bae329
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 62 deletions.
35 changes: 22 additions & 13 deletions prelude/__internal__/Char.mad
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@ export fromShort = extern "madlib__number__shortToChar"
* isDigit('3') // true
*/
isDigit :: Char -> Boolean
export isDigit = (s) =>
s == '0' ||
s == '1' ||
s == '2' ||
s == '3' ||
s == '4' ||
s == '5' ||
s == '6' ||
s == '7' ||
s == '8' ||
s == '9'
export isDigit = (s) => s
== '0'
|| s
== '1'
|| s
== '2'
|| s
== '3'
|| s
== '4'
|| s
== '5'
|| s
== '6'
|| s
== '7'
|| s
== '8'
|| s
== '9'


/**
Expand All @@ -54,8 +63,8 @@ export isDigit = (s) =>
*/
isLetter :: Char -> Boolean
export isLetter = (c) => pipe(
String.pushChar($, ""),
String.match("[a-zA-Z]+")
String.prependChar($, ""),
String.match("[a-zA-Z]+"),
)(c)


Expand Down
70 changes: 43 additions & 27 deletions prelude/__internal__/FilePath.mad
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import String from "String"
import {} from "Char"
import { filter, first, last, drop, dropLast } from "List"
import { fromMaybe, Nothing, Just } from "Maybe"
import { complement, equals, identity, ifElse } from "Function"
import { drop, dropLast, filter, first, last } from "List"
import { Just, Nothing, fromMaybe } from "Maybe"
import String from "String"



Expand All @@ -20,7 +20,7 @@ dropTrailingPathSeparator :: FilePath -> FilePath
export dropTrailingPathSeparator = ifElse(
(path) => path != "/" && String.lastChar(path) == Just('/'),
String.dropLast(1),
identity
identity,
)


Expand All @@ -33,8 +33,11 @@ performSplitPath = (buffer, foundSlash, path) => where(String.firstChar(path)) {

Just(char) =>
foundSlash
? mappend([buffer], performSplitPath(String.pushChar(char, ""), false, String.drop(1, path)))
: performSplitPath(buffer ++ String.pushChar(char, ""), false, String.drop(1, path))
? mappend(
[buffer],
performSplitPath(String.prependChar(char, ""), false, String.drop(1, path)),
)
: performSplitPath(buffer ++ String.prependChar(char, ""), false, String.drop(1, path))
}


Expand All @@ -60,10 +63,21 @@ joinPath :: List FilePath -> FilePath
export joinPath = pipe(
filter(complement(String.isEmpty)),
ifElse(
pipe(first, equals(Just("/"))),
pipe(drop(1), map(dropTrailingPathSeparator), String.join("/"), mappend("/")),
pipe(map(dropTrailingPathSeparator), String.join("/"))
)
pipe(
first,
equals(Just("/")),
),
pipe(
drop(1),
map(dropTrailingPathSeparator),
String.join("/"),
mappend("/"),
),
pipe(
map(dropTrailingPathSeparator),
String.join("/"),
),
),
)


Expand All @@ -80,18 +94,24 @@ export canonicalizePath = pipe(
map(
pipe(
ifElse(
pipe(String.take(2), equals("./")),
pipe(
String.take(2),
equals("./"),
),
String.drop(2),
identity
identity,
),
ifElse(
pipe(String.lastChar, equals(Just('/'))),
pipe(
String.lastChar,
equals(Just('/')),
),
String.replace("([^/]+)/*", "$1/"),
identity
)
)
identity,
),
),
),
joinPath
joinPath,
)


Expand All @@ -107,7 +127,7 @@ dropPathSegments :: Integer -> FilePath -> FilePath
export dropPathSegments = (howMany) => pipe(
splitPath,
drop(howMany),
joinPath
joinPath,
)


Expand All @@ -122,7 +142,7 @@ parentPath :: FilePath -> FilePath
export parentPath = pipe(
splitPath,
dropLast(1),
joinPath
joinPath,
)


Expand All @@ -143,9 +163,7 @@ export isRootPathOf = (root, path) => {
pathStart = dropTrailingPathSeparator(fromMaybe("", first(pathParts)))

return rootStart == pathStart || rootStart == ""
? rootStart == ""
? true
: isRootPathOf(dropPathSegments(1, root), dropPathSegments(1, path))
? rootStart == "" ? true : isRootPathOf(dropPathSegments(1, root), dropPathSegments(1, path))
: false
}

Expand All @@ -164,13 +182,11 @@ export takeFileName = pipe(
last,
where {
Just(part) =>
String.lastChar(part) == Just('/')
? ""
: part
String.lastChar(part) == Just('/') ? "" : part

Nothing =>
""
}
},
)


Expand All @@ -188,5 +204,5 @@ export takeExtension = pipe(
String.split("."),
last,
map(ifElse(String.isEmpty, identity, mappend("."))),
fromMaybe("")
fromMaybe(""),
)
2 changes: 1 addition & 1 deletion prelude/__internal__/Parse.mad
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ string :: String -> Parser String
export string = (s) => where(String.firstChar(s)) {
Just(c) =>
pipe(
map((a, b) => String.pushChar(a, b)),
map((a, b) => String.prependChar(a, b)),
ap($, string(String.drop(1, s))),
)(char(c))

Expand Down
41 changes: 24 additions & 17 deletions prelude/__internal__/String.mad
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { Maybe } from "Maybe"

import List from "List"



#iftarget js

import { Just, Nothing } from "Maybe"



#endif


Expand Down Expand Up @@ -114,7 +119,7 @@ export split = extern "madlib__string__split"
join :: String -> List String -> String
export join = (a, xs) => pipe(
List.intersperse(a),
List.reduce(mappend, "")
List.reduce(mappend, ""),
)(xs)

/**
Expand Down Expand Up @@ -235,7 +240,7 @@ filterChars :: (Char -> Boolean) -> String -> String
export filterChars = (predicate, s) => pipe(
toList,
List.filter(predicate),
fromList
fromList,
)(s)

/**
Expand All @@ -245,7 +250,7 @@ export filterChars = (predicate, s) => pipe(
reduceChars :: (a -> Char -> a) -> a -> String -> a
export reduceChars = (f, initial, s) => pipe(
toList,
List.reduce(f, initial)
List.reduce(f, initial),
)(s)


Expand Down Expand Up @@ -298,7 +303,7 @@ dropWhile :: (Char -> Boolean) -> String -> String
export dropWhile = (predicate, s) => pipe(
toList,
List.dropWhile(predicate),
fromList
fromList,
)(s)

/**
Expand All @@ -323,7 +328,7 @@ takeWhile :: (Char -> Boolean) -> String -> String
export takeWhile = (predicate, s) => pipe(
toList,
List.takeWhile(predicate),
fromList
fromList,
)(s)


Expand Down Expand Up @@ -439,7 +444,7 @@ export length = extern "madlib__string__length"
repeat :: Char -> Integer -> String
export repeat = (c, n) => pipe(
List.repeat(c),
fromList
fromList,
)(n)


Expand All @@ -457,9 +462,11 @@ export match = (regex, input) => #- input.match(regex) !== null -#
* @since 0.18.7
*/
replace :: String -> String -> String -> String
export replace = (regex, replacing, input) => (#-
export replace = (regex, replacing, input) => (
#-
input.replace(new RegExp(regex, "g"), replacing)
-#)
-#
)

#elseif llvm

Expand All @@ -486,8 +493,8 @@ export replace = extern "madlib__string__replace"
* Pushes a char at the beginning of a String
* @since 0.12.0
*/
pushChar :: Char -> String -> String
export pushChar = (c, s) => #- { return c + s } -#
prependChar :: Char -> String -> String
export prependChar = (c, s) => #- { return c + s } -#

/**
* Appends a char at the end of a String
Expand All @@ -502,8 +509,8 @@ export appendChar = (c, s) => #- { return s + c } -#
* pushes a char at the beginning of a String
* @since 0.12.0
*/
pushChar :: Char -> String -> String
export pushChar = extern "madlib__string__pushChar"
prependChar :: Char -> String -> String
export prependChar = extern "madlib__string__prependChar"

/**
* appends a char at the end of a String
Expand All @@ -526,7 +533,7 @@ reverse :: String -> String
export reverse = (s) => pipe(
toList,
List.reverse,
fromList
fromList,
)(s)


Expand All @@ -540,7 +547,7 @@ export reverse = (s) => pipe(
includes :: Char -> String -> Boolean
export includes = (c, s) => pipe(
toList,
List.includes(c)
List.includes(c),
)(s)


Expand All @@ -555,7 +562,7 @@ export includes = (c, s) => pipe(
startsWith :: String -> String -> Boolean
export startsWith = (subset, s) => pipe(
toList,
List.startsWith(toList(subset))
List.startsWith(toList(subset)),
)(s)


Expand All @@ -570,7 +577,7 @@ export startsWith = (subset, s) => pipe(
contains :: String -> String -> Boolean
export contains = (subset, s) => pipe(
toList,
List.contains(toList(subset))
List.contains(toList(subset)),
)(s)


Expand All @@ -585,5 +592,5 @@ export contains = (subset, s) => pipe(
endsWith :: String -> String -> Boolean
export endsWith = (subset, s) => pipe(
toList,
List.endsWith(toList(subset))
List.endsWith(toList(subset)),
)(s)
5 changes: 3 additions & 2 deletions prelude/__internal__/String.spec.mad
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {} from "Char"
import { EQ, LT, GT, eq } from "Compare"
import { EQ, GT, LT, eq } from "Compare"
import { Just } from "Maybe"
import { assertEquals, test } from "Test"

import String from "./String"



test("compare String - GT", () => assertEquals(compare("John", "James"), GT))
test("compare String - LT", () => assertEquals(compare("John", "Paul"), LT))
test("compare String - EQ", () => assertEquals(compare("John", "John"), EQ))
Expand Down Expand Up @@ -126,7 +127,7 @@ test("repeat", () => assertEquals(String.repeat('a', 10), "aaaaaaaaaa"))
test("toUpper", () => assertEquals(String.toUpper("atwzö2êé臋©Ïƒ¬"), "ATWZÖ2ÊÉȇ‹©ÏƑ¬"))
test("toLower", () => assertEquals(String.toLower("ATWZÖ2ÊÉȇ‹©ÏƑ¬"), "atwzö2êé臋©ïƒ¬"))

test("pushChar", () => assertEquals(String.pushChar('a', "bc"), "abc"))
test("prependChar", () => assertEquals(String.prependChar('a', "bc"), "abc"))
test("appendChar", () => assertEquals(String.appendChar('c', "ab"), "abc"))

test("reverse", () => assertEquals(String.reverse("abc"), "cba"))
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ char *madlib__string__slice(int64_t start, int64_t end, unsigned char *s) {
return result;
}

char *madlib__string__pushChar(int32_t c, char* s) {
char *madlib__string__prependChar(int32_t c, char* s) {
char *encoded = utf8EncodeChar(c);
size_t encodedLength = strlen(encoded);
size_t stringLength = strlen(s);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int64_t madlib__string__length(unsigned char *s);

char *madlib__string__slice(int64_t start, int64_t end, unsigned char *s);

char *madlib__string__pushChar(int32_t c, char* s);
char *madlib__string__prependChar(int32_t c, char* s);
char *madlib__string__appendChar(int32_t c, char* s);

char *madlib__string__trim(char *s);
Expand Down

0 comments on commit 3bae329

Please sign in to comment.