Library Stdlib.Strings.String
Contributed by Laurent Théry (INRIA);
Adapted to Coq V8 by the Coq Development Team
From Stdlib Require Import Arith.
From Stdlib Require Import Ascii.
From Stdlib Require Import Bool.
From Stdlib.Strings Require Import Byte.
Import IfNotations.
Definition of strings
Implementation of string as list of ascii characters
Inductive string :
Set :=
|
EmptyString :
string
|
String :
ascii -> string -> string.
Declare Scope string_scope.
Delimit Scope string_scope with string.
Bind Scope string_scope with string.
Local Open Scope string_scope.
Register string as core.string.type.
Register EmptyString as core.string.empty.
Register String as core.string.string.
Equality is decidable
Compare strings lexicographically
Length
Nth character of a string
Two lists that are identical through get are syntactically equal
The first elements of s1 ++ s2 are the ones of s1
The last elements of s1 ++ s2 are the ones of s2
Substrings
substring n m s returns the substring of
s that starts
at position
n and of length
m;
if this does not make sense it returns
""
Fixpoint substring (
n m :
nat) (
s :
string) :
string :=
match n,
m,
s with
|
O,
O,
_ =>
EmptyString
|
O,
S m',
EmptyString =>
s
|
O,
S m',
String c s' =>
String c (
substring 0
m' s')
|
S n',
_,
EmptyString =>
s
|
S n',
_,
String c s' =>
substring n' m s'
end.
The substring is included in the initial string
The substring has at most m elements
Concatenating lists of strings
concat sep sl concatenates the list of strings
sl, inserting
the separator string
sep between each.
Test functions
Test if
s1 is a prefix of
s2
If s1 is a prefix of s2, it is the substring of length
length s1 starting at position O of s2
Test if, starting at position n, s1 occurs in s2; if
so it returns the position
If the result of index is Some m, s1 in s2 at position m
If the result of index is Some m,
s1 does not occur in s2 before m
If the result of index is None, s1 does not occur in s2
after n
If we are searching for the Empty string and the answer is no
this means that n is greater than the size of s
Same as index but with no optional type, we return 0 when it
does not occur
Conversion to/from list ascii and list byte
Concrete syntax
The concrete syntax for strings in scope string_scope follows the
Coq convention for strings: all ascii characters of code less than
128 are literals to the exception of the character `double quote'
which must be doubled.
Strings that involve ascii characters of code >= 128 which are not
part of a valid utf8 sequence of characters are not representable
using the Coq string notation (use explicitly the String constructor
with the ascii codes of the characters).