diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | gen/src/gleam@list.erl | 4 | ||||
-rw-r--r-- | gen/src/gleam@string.erl | 12 | ||||
-rw-r--r-- | src/gleam/iodata.gleam | 2 | ||||
-rw-r--r-- | src/gleam/string.gleam | 293 |
5 files changed, 65 insertions, 248 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c27bcb..5d42a67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- The `string.replace` and `iodata.replace` `all` arguement label has been + changed to `each`. - The `string` module gains `is_empty`, `join` and `concat` functions. - The `int` module gains `is_even` and `is_odd` functions. diff --git a/gen/src/gleam@list.erl b/gen/src/gleam@list.erl index 5706993..57789ff 100644 --- a/gen/src/gleam@list.erl +++ b/gen/src/gleam@list.erl @@ -258,8 +258,8 @@ intersperse(List, Elem) -> [_] -> List; - [X1 | Rest] -> - [X1, Elem | intersperse(Rest, Elem)] + [X | Rest] -> + [X, Elem | intersperse(Rest, Elem)] end. at(List, Index) -> diff --git a/gen/src/gleam@string.erl b/gen/src/gleam@string.erl index eef6f5e..a09e7e4 100644 --- a/gen/src/gleam@string.erl +++ b/gen/src/gleam@string.erl @@ -4,13 +4,7 @@ -export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, split/2, append/2, concat/1, join/2]). is_empty(Str) -> - case Str of - <<"">> -> - true; - - _ -> - false - end. + Str =:= <<"">>. length(A) -> string:length(A). @@ -32,9 +26,9 @@ uppercase(A) -> compare(A, B) -> gleam_stdlib:compare_strings(A, B). -split(X, Pattern) -> +split(X, Substring) -> gleam@list:map( - gleam@iodata:split(gleam@iodata:new(X), Pattern), + gleam@iodata:split(gleam@iodata:new(X), Substring), fun gleam@iodata:to_string/1 ). diff --git a/src/gleam/iodata.gleam b/src/gleam/iodata.gleam index 264bbbe..821581e 100644 --- a/src/gleam/iodata.gleam +++ b/src/gleam/iodata.gleam @@ -52,7 +52,7 @@ external fn erl_replace(Iodata, String, String, Direction) -> Iodata = pub fn replace( in iodata: Iodata, - all pattern: String, + each pattern: String, with substitute: String, ) -> Iodata { erl_replace(iodata, pattern, substitute, All) diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 67459b7..fc0f735 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -4,15 +4,10 @@ import gleam/iodata import gleam/list import gleam/order - - - +import gleam/result.{Option} /// ## Basics - - - /// Determine if a string is empty. /// /// ## Examples @@ -22,14 +17,10 @@ import gleam/order /// ``` /// pub fn is_empty(str: String) -> Bool { - case str { - "" -> True - _ -> False - } + str == "" } - -/// Get the length of a +/// Get the length of a /// /// ## Examples /// ```gleam @@ -38,16 +29,14 @@ pub fn is_empty(str: String) -> Bool { /// pub external fn length(String) -> Int = "string" "length" - /// Repeat a string `n` times. /// /// ## Examples /// ```gleam -/// repeat(3, "ha") == "hahaha" +/// repeat("ha", times: 3) == "hahaha" /// ``` /// -// pub fn repeat(string: String, times: Int) -> String {} - +// pub fn repeat(string: String, times n: Int) -> String {} /// Reverse a string. /// @@ -63,27 +52,25 @@ pub fn reverse(string: String) -> String { |> iodata.to_string } - /// Replace all occurrences of some substring. /// /// ## Examples /// ```gleam -/// replace("Json.Decode.succeed", all: ".", with: "-") == "Json-Decode-succeed" -/// replace("a,b,c,d,e", all: ",", with: "/") == "a/b/c/d/e" +/// replace("Json.Decode.succeed", each: ".", with: "-") == "Json-Decode-succeed" +/// replace("a,b,c,d,e", each: ",", with: "/") == "a/b/c/d/e" /// ``` /// pub fn replace( in string: String, - all pattern: String, + each pattern: String, with substitute: String, ) -> String { string |> iodata.new - |> iodata.replace(_, all: pattern, with: substitute) + |> iodata.replace(_, each: pattern, with: substitute) |> iodata.to_string } - /// Convert a string to all lower case. Useful for case-insensitive comparisons. /// /// ## Examples @@ -93,7 +80,6 @@ pub fn replace( /// pub external fn lowercase(String) -> String = "string" "lowercase" - /// Convert a string to all upper case. Useful for case-insensitive comparisons /// and VIRTUAL YELLING. /// @@ -104,121 +90,85 @@ pub external fn lowercase(String) -> String = "string" "lowercase" /// pub external fn uppercase(String) -> String = "string" "uppercase" - /// Determines the order of the two strings. /// /// ## Examples /// ```gleam /// compare("Anthony", "Anthony") == order.Eq +/// compare("A", "B") == order.Gt /// ``` /// pub external fn compare(String, String) -> order.Order = "gleam_stdlib" "compare_strings" - - - - /// ## Get Substrings - - - -/// Take a substring given a start and end index. Negative indexes +/// Take a substring given a start and end Grapheme indexes. Negative indexes /// are taken starting from the *end* of the list. /// /// ## Examples /// ```gleam -/// slice(start: -6, end: -1, "snakes on a plane!") == "plane" -/// ``` -/// -// pub fn slice(string: String, start: Int, end: Int) -> String {} - - -/// Take *n* characters from the left side of a -/// -/// ## Examples -/// ```gleam -/// left(num: 2, string: "Mulder") == "Mu" +/// slice("gleam", from: 1, to: 3) == "lea" +/// slice("gleam", from: 1, to: 10) == "leam" +/// slice("snakes on a plane!", from: -6, to: -1) == "plane" /// ``` /// -// pub fn left(from string: String, num n: Int) -> String {} +// pub fn slice(out_of string: String, from start: Int, end: Int) -> String {} - -/// Take *n* characters from the right side of a +/// Drop *n* Graphemes from the left side of a /// /// ## Examples /// ```gleam -/// right("Scully", 2) == "ly" +/// drop_left(from: "The Lone Gunmen", up_to: 2) == "e Lone Gunmen" /// ``` /// -// pub fn right(from string: String, num_characters: Int) -> String {} - +// pub fn drop_left(from string: String, up_to num_graphemes: Int) -> String {} -/// Drop *n* characters from the left side of a +/// Drop *n* Graphemes from the right side of a /// /// ## Examples /// ```gleam -/// drop_left(from: "The Lone Gunmen", num_characters: 2) == "e Lone Gunmen" +/// drop_right(from: "Cigarette Smoking Man", up_to: 2) == "Cigarette Smoking M" /// ``` /// -// pub fn drop_left(from string: String, num_characters: Int) -> String {} - - -/// Drop *n* characters from the right side of a -/// -/// ## Examples -/// ```gleam -/// drop_right("Cigarette Smoking Man", 2) == "Cigarette Smoking M" -/// ``` -/// -// pub fn drop_right(from string: String, num_characters: Int) -> String {} - - - +// pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String {} /// ## Check for Substrings - - - +// TODO: Not sure about the name and labels here /// See if the second string contains the first one. /// /// ## Examples /// ```gleam -/// contains("theory", this: "THE") == False +/// contains(does: "theory", contain: "ory") == True +/// contains(does: "theory", contain: "the") == True +/// contains(does: "theory", contain: "THE") == False /// ``` /// -// pub fn contains(this: String, in: String) -> String {} - +// pub fn contains(does haystack: String, contain needle: String) -> String {} +// TODO: Not sure about the name and labels here /// See if the second string starts with the first one. /// /// ## Examples /// ```gleam -/// starts_with("theory", this: "ory") == False +/// starts_with(does: "theory", start_with: "ory") == False /// ``` /// -// pub fn starts_with(this: String, in: String) -> String {} - +// pub fn starts_with(does string: String, start_with prefix: String) -> String {} +// TODO: Not sure about the name and labels here /// See if the second string ends with the first one. /// /// ## Examples /// ```gleam -/// endsWith("theory", this: "ory") == True +/// ends_with(does: "theory", end_with: "ory") == True /// ``` /// -// pub fn ends_with(this: String, in: String) -> String {} - - - +// pub fn ends_with(does string: String, end_with suffix: String) -> String {} /// ## Building and Splitting - - - /// Split a string using a given separator. /// /// ## Examples @@ -226,10 +176,10 @@ pub external fn compare(String, String) -> order.Order = /// split("home/evan/Desktop/", on: "/") == ["home","evan","Desktop", ""] /// ``` /// -pub fn split(string x: String, on pattern: String) -> List(String) { +pub fn split(x: String, on substring: String) -> List(String) { x |> iodata.new - |> iodata.split(_, on: pattern) + |> iodata.split(_, on: substring) |> list.map(_, with: iodata.to_string) } @@ -248,12 +198,11 @@ pub fn append(to first: String, suffix second: String) -> String { |> iodata.to_string } - /// Concatenate many strings into one. /// /// ## Examples /// ```gleam -/// concat(["never","the","less"]) == "nevertheless" +/// concat(["never", "the", "less"]) == "nevertheless" /// ``` /// pub fn concat(strings: List(String)) -> String { @@ -262,8 +211,7 @@ pub fn concat(strings: List(String)) -> String { |> iodata.to_string } - -/// Put many strings together with a given separator. +/// Join many strings together with a given separator. /// /// ## Examples /// ```gleam @@ -277,76 +225,31 @@ pub fn join(strings: List(String), with separator: String) -> String { |> iodata.to_string } - - -/// Break a string into words, splitting on chunks of whitespace. -/// -/// ## Examples -/// ```gleam -/// words("How are \t you? \n Good?") == ["How","are","you?","Good?"] -/// ``` -/// -// pub fn words(string: String) -> List(String) {} - - -/// Break a string into lines, splitting on newlines. -/// -/// ## Examples -/// ```gleam -/// lines("How are you?\nGood?") == ["How are you?", "Good?"] -/// ``` -/// -// pub fn lines(string: String): List(String) {} - - -/// Get all of the indexes for a substring in another -/// -/// ## Examples -/// ```gleam -/// indexes(of: "needle", in: "haystack") == [] -/// ``` -/// -// pub fn indexes(of: String, in: String) -> String {} - - - - /// ## Formatting - - - -/// Pad a string on both sides until it has a given length. +/// Pad a string on the left until it has at least given number of Graphemes. /// /// ## Examples /// ```gleam -/// pad("121", to: 5, with: ' ') == " 121 " -/// ``` -/// -// pub fn pad(string: String, to size: Int, with: String) -> String {} - - -/// Pad a string on the left until it has a given length. -/// -/// ## Examples -/// ```gleam -/// padLeft("121", to: 5, with: '.') == "..121" +/// pad_left("121", to: 5, with: ".") == "..121" +/// pad_left("121", to: 3, with: ".") == "121" +/// pad_left("121", to: 2, with: ".") == "121" /// ``` /// // pub fn pad_left(string: String, to size: Int, with: String) {} - /// Pad a string on the right until it has a given length. /// /// ## Examples /// ```gleam -/// padRight("121", to: 5, with: '.') == "121.." +/// pad_right("121", to: 5, with: ".") == "121.." +/// pad_right("121", to: 3, with: ".") == "121" +/// pad_right("121", to: 2, with: ".") == "121" /// ``` /// // pub fn pad_right(string: String, to size: Int, with: String) {} - -/// Get rid of whitespace on both sides of a +/// Get rid of whitespace on both sides of a String. /// /// ## Examples /// ```gleam @@ -355,131 +258,49 @@ pub fn join(strings: List(String), with separator: String) -> String { /// // pub fn trim(string: String) -> String {} - -/// Get rid of whitespace on the left of a +/// Get rid of whitespace on the left of a String. /// /// ## Examples /// ```gleam -/// trimLeft(" hats \n") == "hats \n" +/// trim_left(" hats \n") == "hats \n" /// ``` /// // pub fn trim_left(string: String) -> String {} - -/// Get rid of whitespace on the right of a +/// Get rid of whitespace on the right of a String. /// /// ## Examples /// ```gleam -/// trimRight(" hats \n") == " hats" +/// trim_right(" hats \n") == " hats" /// ``` /// // pub fn trim_right(string: String) -> String {} +/// ## Grapheme Conversions - - - -/// ## List Conversions - - - - -// These functions convert to and from char, which currently +// These functions convert to and from Grapheme, which currently // does not exist as a type in Gleam. -// /// Convert a string to a list of characters. +// /// Convert a string to a list of Graphemes. // /// -// /// to_list("abc") == ['a','b','c'] +// /// to_graphemes("abc") == ['a','b','c'] // /// -// pub fn to_list(string: String) -> List(String) {} +// pub fn to_graphemes(string: String) -> List(String) {} -// /// Convert a list of characters into a Can be useful if you +// /// Convert a list of characters into a String. Can be useful if you // /// want to create a string primarily by consing, perhaps for decoding // /// something. // /// // /// from_list(['a','b','c']) == "abc" // /// -// // pub fn from_list(strings: List(String)) -> String {} - -// /// Add a character to the beginning of a string. -// /// -// /// cons('T', onto: "he truth is out there") == "The truth is out there" -// // pub fn cons(char: Char, onto string: String) -> String {} - +// // pub fn from_graphemes(graphemes: List(Grapheme)) -> String {} /// Split a non-empty string into its head and tail. This lets you /// pattern match on strings exactly as you would with lists. /// /// ## Examples /// ```gleam -/// uncons("") == Error(Nil) -/// ``` -/// -// pub fn uncons(string: String) -> Result(tuple(String, String), Nil) {} - - - - -/// ## Higher-Order Functions - - - - -/// Transform every character in a string -/// -/// ## Examples -/// ```gleam -/// map("a/b/c", with: replace(all: "/", with: ".")) == "a.b.c" -/// ``` -/// -// pub fn map(string: String, with: fn(String) -> String) -> String {} - - -/// Keep only the characters that pass the test. -/// -/// ## Examples -/// ```gleam -/// filter("R2-D2", where: isDigit) == "22" -/// ``` -/// -// pub fn filter(string: String, where: fn(String) -> Bool) -> String {} - - -/// Reduce a string from the left. -/// -/// ## Examples -/// ```gleam -/// foldl("time", into: "", with: append) == "emit" -/// ``` -/// -// pub fn foldl(string: String, into accumulator: b, with: fn(String, b) -> b) -> b {} - - -/// Reduce a string from the right. -/// -/// ## Examples -/// ```gleam -/// foldr("time", into: "", with: append) == "time" -/// ``` -/// -// pub fn foldr(string: String, into accumulator: b, with: fn(String, b) -> b) -> b {} - - -/// Determine whether *any* characters pass the test. -/// -/// ## Examples -/// ```gleam -/// any("heart", that: isDigit) == False -/// ``` -/// -// pub fn any(string: String, that predicate: fn(String) -> Bool) -> Bool {} - - -/// Determine whether *all* characters pass the test. -/// -/// ## Examples -/// ```gleam -/// all("heart", that: isDigit) == False +/// next_grapheme("") == Error(Nil) /// ``` /// -// pub fn all(string: String, that predicate: fn(String) -> Bool) -> Bool {} +// pub fn next_grapheme(string: String) -> Option(tuple(Grapheme, String)) {} |