| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Strings: builtin type and basic operations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.String.Base where |
| 10 |
|
| 11 |
open import Data.Bool.Base using (Bool; true; false; if_then_else_) |
| 12 |
open import Data.Char.Base as Char using (Char) |
| 13 |
open import Data.List.Base as List using (List; [_]; _∷_; []) |
| 14 |
open import Data.List.NonEmpty.Base as NE using (List⁺) |
| 15 |
open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise) |
| 16 |
open import Data.List.Relation.Binary.Lex.Core using (Lex-<; Lex-≤) |
| 17 |
open import Data.Maybe.Base as Maybe using (Maybe) |
| 18 |
open import Data.Nat.Base using (ℕ; _∸_; ⌊_/2⌋; ⌈_/2⌉; _≡ᵇ_) |
| 19 |
open import Data.Product.Base using (proj₁; proj₂) |
| 20 |
open import Function.Base using (_on_; _∘′_; _∘_) |
| 21 |
open import Level using (Level; 0ℓ) |
| 22 |
open import Relation.Binary.Core using (Rel) |
| 23 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 24 |
open import Relation.Unary using (Pred; Decidable) |
| 25 |
open import Relation.Nullary.Decidable.Core using (does; T?) |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- From Agda.Builtin: type and renamed primitives |
| 29 |
|
| 30 |
-- Note that we do not re-export primStringAppend because we want to |
| 31 |
-- give it an infix definition and be able to assign it a level. |
| 32 |
|
| 33 |
import Agda.Builtin.String as String |
| 34 |
|
| 35 |
open String public using ( String ) |
| 36 |
renaming |
| 37 |
( primStringUncons to uncons |
| 38 |
; primStringToList to toList |
| 39 |
; primStringFromList to fromList |
| 40 |
; primShowString to show |
| 41 |
) |
| 42 |
|
| 43 |
------------------------------------------------------------------------ |
| 44 |
-- Relations |
| 45 |
|
| 46 |
-- Pointwise equality on Strings |
| 47 |
|
| 48 |
infix 4 _≈_ |
| 49 |
_≈_ : Rel String 0ℓ |
| 50 |
_≈_ = Pointwise _≡_ on toList |
| 51 |
|
| 52 |
-- Lexicographic ordering on Strings |
| 53 |
|
| 54 |
infix 4 _<_ |
| 55 |
_<_ : Rel String 0ℓ |
| 56 |
_<_ = Lex-< _≡_ Char._<_ on toList |
| 57 |
|
| 58 |
infix 4 _≤_ |
| 59 |
_≤_ : Rel String 0ℓ |
| 60 |
_≤_ = Lex-≤ _≡_ Char._<_ on toList |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Operations |
| 64 |
|
| 65 |
-- List-like operations |
| 66 |
|
| 67 |
head : String → Maybe Char |
| 68 |
head = Maybe.map proj₁ ∘′ uncons |
| 69 |
|
| 70 |
tail : String → Maybe String |
| 71 |
tail = Maybe.map proj₂ ∘′ uncons |
| 72 |
|
| 73 |
-- Additional conversion functions |
| 74 |
|
| 75 |
fromChar : Char → String |
| 76 |
fromChar = fromList ∘′ [_] |
| 77 |
|
| 78 |
fromList⁺ : List⁺ Char → String |
| 79 |
fromList⁺ = fromList ∘′ NE.toList |
| 80 |
|
| 81 |
-- List-like functions |
| 82 |
|
| 83 |
infixr 5 _++_ |
| 84 |
_++_ : String → String → String |
| 85 |
_++_ = String.primStringAppend |
| 86 |
|
| 87 |
length : String → ℕ |
| 88 |
length = List.length ∘ toList |
| 89 |
|
| 90 |
replicate : ℕ → Char → String |
| 91 |
replicate n = fromList ∘ List.replicate n |
| 92 |
|
| 93 |
concat : List String → String |
| 94 |
concat = List.foldr _++_ "" |
| 95 |
|
| 96 |
intersperse : String → List String → String |
| 97 |
intersperse sep = concat ∘′ (List.intersperse sep) |
| 98 |
|
| 99 |
unwords : List String → String |
| 100 |
unwords = intersperse " " |
| 101 |
|
| 102 |
unlines : List String → String |
| 103 |
unlines = intersperse "\n" |
| 104 |
|
| 105 |
between : String → String → String → String |
| 106 |
between left right middle = left ++ middle ++ right |
| 107 |
|
| 108 |
parens : String → String |
| 109 |
parens = between "(" ")" |
| 110 |
|
| 111 |
braces : String → String |
| 112 |
braces = between "{" "}" |
| 113 |
|
| 114 |
-- append that also introduces spaces, if necessary |
| 115 |
infixr 5 _<+>_ |
| 116 |
_<+>_ : String → String → String |
| 117 |
"" <+> b = b |
| 118 |
a <+> "" = a |
| 119 |
a <+> b = a ++ " " ++ b |
| 120 |
|
| 121 |
------------------------------------------------------------------------ |
| 122 |
-- Padding |
| 123 |
|
| 124 |
-- Each one of the padding functions should verify the following |
| 125 |
-- invariant: |
| 126 |
-- If length str ≤ n then length (padLeft c n str) ≡ n |
| 127 |
-- and otherwise padLeft c n str ≡ str. |
| 128 |
|
| 129 |
-- Appending an empty string is expensive (append for Haskell's |
| 130 |
-- Text creates a fresh Text value in which both contents are |
| 131 |
-- copied) so we precompute `n ∸ length str` and check whether |
| 132 |
-- it is equal to 0. |
| 133 |
|
| 134 |
padLeft : Char → ℕ → String → String |
| 135 |
padLeft c n str = |
| 136 |
let l = n ∸ length str in |
| 137 |
if l ≡ᵇ 0 then str else replicate l c ++ str |
| 138 |
|
| 139 |
padRight : Char → ℕ → String → String |
| 140 |
padRight c n str = |
| 141 |
let l = n ∸ length str in |
| 142 |
if l ≡ᵇ 0 then str else str ++ replicate l c |
| 143 |
|
| 144 |
padBoth : Char → Char → ℕ → String → String |
| 145 |
padBoth cₗ cᵣ n str = |
| 146 |
let l = n ∸ length str in |
| 147 |
if l ≡ᵇ 0 then str else replicate ⌊ l /2⌋ cₗ ++ str ++ replicate ⌈ l /2⌉ cᵣ |
| 148 |
|
| 149 |
------------------------------------------------------------------------ |
| 150 |
-- Alignment |
| 151 |
|
| 152 |
-- We can align a String left, center or right in a column of a given |
| 153 |
-- width by padding it with whitespace. |
| 154 |
|
| 155 |
data Alignment : Set where |
| 156 |
Left Center Right : Alignment |
| 157 |
|
| 158 |
fromAlignment : Alignment → ℕ → String → String |
| 159 |
fromAlignment Left = padRight ' ' |
| 160 |
fromAlignment Center = padBoth ' ' ' ' |
| 161 |
fromAlignment Right = padLeft ' ' |
| 162 |
|
| 163 |
------------------------------------------------------------------------ |
| 164 |
-- Splitting strings |
| 165 |
|
| 166 |
wordsBy : ∀ {p} {P : Pred Char p} → Decidable P → String → List String |
| 167 |
wordsBy P? = List.map fromList ∘ List.wordsBy P? ∘ toList |
| 168 |
|
| 169 |
wordsByᵇ : (Char → Bool) → String → List String |
| 170 |
wordsByᵇ p = wordsBy (T? ∘ p) |
| 171 |
|
| 172 |
words : String → List String |
| 173 |
words = wordsByᵇ Char.isSpace |
| 174 |
|
| 175 |
-- `words` ignores contiguous whitespace |
| 176 |
_ : words " abc b " ≡ "abc" ∷ "b" ∷ [] |
| 177 |
_ = refl |
| 178 |
|
| 179 |
linesBy : ∀ {p} {P : Pred Char p} → Decidable P → String → List String |
| 180 |
linesBy P? = List.map fromList ∘ List.linesBy P? ∘ toList |
| 181 |
|
| 182 |
linesByᵇ : (Char → Bool) → String → List String |
| 183 |
linesByᵇ p = linesBy (T? ∘ p) |
| 184 |
|
| 185 |
lines : String → List String |
| 186 |
lines = linesByᵇ ('\n' Char.≈ᵇ_) |
| 187 |
|
| 188 |
-- `lines` preserves empty lines |
| 189 |
_ : lines "\nabc\n\nb\n\n\n" ≡ "" ∷ "abc" ∷ "" ∷ "b" ∷ "" ∷ "" ∷ [] |
| 190 |
_ = refl |
| 191 |
|
| 192 |
map : (Char → Char) → String → String |
| 193 |
map f = fromList ∘ List.map f ∘ toList |
| 194 |
|
| 195 |
_ : map Char.toUpper "abc" ≡ "ABC" |
| 196 |
_ = refl |