File: string.gleam

package info (click to toggle)
cloc 2.04-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,776 kB
  • sloc: perl: 29,368; cpp: 1,219; ansic: 334; asm: 267; makefile: 240; sh: 186; sql: 144; java: 136; ruby: 111; cs: 104; python: 84; pascal: 52; lisp: 50; cobol: 35; f90: 35; haskell: 35; objc: 25; php: 22; javascript: 15; fortran: 9; ml: 8; xml: 7; tcl: 2
file content (68 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// https://github.com/gleam-lang/stdlib/raw/main/src/gleam/string.gleam
//// Strings in Gleam are UTF-8 binaries. They can be written in your code a
//// text surrounded by `"double quotes"`.

import gleam/string_builder
import gleam/dynamic.{Dynamic}
import gleam/iterator
import gleam/list
import gleam/order
import gleam/result

pub type String =
  String

/// A UtfCodepoint is the integer representation of a valid UTF codepoint
pub type UtfCodepoint =
  UtfCodepoint

/// Determine if a string is empty.
///
/// ## Examples
///
///    > is_empty("")
///    True
///
///    > is_empty("the world")
///    False
///
pub fn is_empty(str: String) -> Bool {
  str == ""
}

/// Get the number of grapheme clusters in a given string.
///
/// This function has to iterate across the whole string to count the number of
/// graphemes, so it runs in linear time.
///
/// ## Examples
///
///    > length("Gleam")
///    5
///
///    > length("ß↑e̊")
///    3
///
///    > length("")
///    0
///
pub external fn length(String) -> Int =
  "string" "length"

///
/// Reverse a string.
///
/// This function has to iterate across the whole string so it runs in linear
/// time.
///
/// ## Examples
///
///    > reverse("stressed")
///    "desserts"
///
pub fn reverse(string: String) -> String {
  string
  |> string_builder.from_string
  |> string_builder.reverse
  |> string_builder.to_string
}