{-# OPTIONS --without-K --safe #-}
module Data.String.Base where
open import Level using (zero)
open import Data.Bool using (true; false)
open import Data.Nat.Base as ℕ using (ℕ; _∸_; ⌊_/2⌋; ⌈_/2⌉)
import Data.Nat.Properties as ℕₚ
open import Data.List.Base as List using (List; [_])
open import Data.List.NonEmpty as NE using (List⁺)
open import Data.List.Extrema ℕₚ.≤-totalOrder
open import Data.List.Relation.Binary.Pointwise using (Pointwise)
open import Data.List.Relation.Binary.Lex.Strict using (Lex-<)
open import Data.Vec.Base as Vec using (Vec)
open import Data.Char.Base as Char using (Char)
import Data.Char.Properties as Char using (_≟_)
open import Function
open import Relation.Binary using (Rel)
open import Relation.Nullary using (does)
open import Data.List.Membership.DecPropositional Char._≟_
import Agda.Builtin.String as String
open String public using ( String )
renaming
( primStringToList to toList
; primStringFromList to fromList
; primShowString to show
)
infix 4 _≈_
_≈_ : Rel String zero
_≈_ = Pointwise Char._≈_ on toList
infix 4 _<_
_<_ : Rel String zero
_<_ = Lex-< Char._≈_ Char._<_ on toList
fromChar : Char → String
fromChar = fromList ∘′ [_]
fromList⁺ : List⁺ Char → String
fromList⁺ = fromList ∘′ NE.toList
infixr 5 _++_
_++_ : String → String → String
_++_ = String.primStringAppend
length : String → ℕ
length = List.length ∘ toList
replicate : ℕ → Char → String
replicate n = fromList ∘ List.replicate n
concat : List String → String
concat = List.foldr _++_ ""
intersperse : String → List String → String
intersperse sep = concat ∘′ (List.intersperse sep)
unlines : List String → String
unlines = intersperse "\n"
unwords : List String → String
unwords = intersperse " "
parens : String → String
parens s = "(" ++ s ++ ")"
parensIfSpace : String → String
parensIfSpace s with does (' ' ∈? toList s)
... | true = parens s
... | false = s
braces : String → String
braces s = "{" ++ s ++ "}"
infixr 5 _<+>_
_<+>_ : String → String → String
"" <+> b = b
a <+> "" = a
a <+> b = a ++ " " ++ b
padLeft : Char → ℕ → String → String
padLeft c n str with n ∸ length str
... | 0 = str
... | l = replicate l c ++ str
padRight : Char → ℕ → String → String
padRight c n str with n ∸ length str
... | 0 = str
... | l = str ++ replicate l c
padBoth : Char → Char → ℕ → String → String
padBoth cₗ cᵣ n str with n ∸ length str
... | 0 = str
... | l = replicate ⌊ l /2⌋ cₗ ++ str ++ replicate ⌈ l /2⌉ cᵣ
data Alignment : Set where
Left Center Right : Alignment
fromAlignment : Alignment → ℕ → String → String
fromAlignment Left = padRight ' '
fromAlignment Center = padBoth ' ' ' '
fromAlignment Right = padLeft ' '
rectangle : ∀ {n} → Vec (ℕ → String → String) n →
Vec String n → Vec String n
rectangle pads cells = Vec.zipWith (λ p c → p width c) pads cells where
sizes = List.map length (Vec.toList cells)
width = max 0 sizes
rectangleˡ : ∀ {n} → Char → Vec String n → Vec String n
rectangleˡ c = rectangle (Vec.replicate $ padLeft c)
rectangleʳ : ∀ {n} → Char → Vec String n → Vec String n
rectangleʳ c = rectangle (Vec.replicate $ padRight c)
rectangleᶜ : ∀ {n} → Char → Char → Vec String n → Vec String n
rectangleᶜ cₗ cᵣ = rectangle (Vec.replicate $ padBoth cₗ cᵣ)