{-# OPTIONS --cubical-compatible --safe #-}
module Data.String where
open import Data.Bool.Base using (if_then_else_)
open import Data.Char.Base as Char using (Char)
open import Function.Base using (_∘_; _$_)
open import Data.Nat.Base as ℕ using (ℕ)
import Data.Nat.Properties as ℕ
open import Data.List.Base as List using (List)
open import Data.List.Extrema ℕ.≤-totalOrder
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 Relation.Nullary.Decidable.Core using (does)
open import Data.List.Membership.DecPropositional Char._≟_
open import Data.String.Base public
open import Data.String.Properties using (_≈?_; _≟_; _<?_; _==_) public
toVec : (s : String) → Vec Char (length s)
toVec s = Vec.fromList (toList s)
fromVec : ∀ {n} → Vec Char n → String
fromVec = fromList ∘ Vec.toList
parensIfSpace : String → String
parensIfSpace s = if does (' ' ∈? toList s) then parens s else s
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ᵣ)