1 /********************* */
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Andres Noetzli
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief Utility functions for words.
15 #include "cvc4_private.h"
17 #ifndef CVC4__THEORY__STRINGS__WORD_H
18 #define CVC4__THEORY__STRINGS__WORD_H
22 #include "expr/node.h"
23 #include "expr/type_node.h"
29 // ------------------------------ for words (string or sequence constants)
33 /** make empty constant of type tn */
34 static Node
mkEmptyWord(TypeNode tn
);
36 /** make word from constants in (non-empty) vector vec */
37 static Node
mkWordFlatten(const std::vector
<Node
>& xs
);
39 /** Return the length of word x */
40 static size_t getLength(TNode x
);
44 * Given word x, this returns the vector of words of length one whose
45 * concatenation is equivalent to x.
47 static std::vector
<Node
> getChars(TNode x
);
49 /** Return true if x is empty */
50 static bool isEmpty(TNode x
);
54 * Returns true if x is equal to y for their first n characters.
55 * If n is larger than the length of x or y, this method returns
56 * true if and only if x is equal to y.
58 static bool strncmp(TNode x
, TNode y
, std::size_t n
);
60 /** reverse string compare
62 * Returns true if x is equal to y for their last n characters.
63 * If n is larger than the length of tx or y, this method returns
64 * true if and only if x is equal to y.
66 static bool rstrncmp(TNode x
, TNode y
, std::size_t n
);
68 /** Return the first position y occurs in x, or std::string::npos otherwise */
69 static std::size_t find(TNode x
, TNode y
, std::size_t start
= 0);
72 * Return the first position y occurs in x searching from the end of x, or
73 * std::string::npos otherwise
75 static std::size_t rfind(TNode x
, TNode y
, std::size_t start
= 0);
77 /** Returns true if y is a prefix of x */
78 static bool hasPrefix(TNode x
, TNode y
);
80 /** Returns true if y is a suffix of x */
81 static bool hasSuffix(TNode x
, TNode y
);
83 /** Replace the character at index n in x with t */
84 static Node
update(TNode x
, std::size_t n
, TNode t
);
86 /** Replace the first occurrence of y in x with t */
87 static Node
replace(TNode x
, TNode y
, TNode t
);
89 /** Return the substring/subsequence of x starting at index i */
90 static Node
substr(TNode x
, std::size_t i
);
92 /** Return the substring/subsequence of x starting at index i with size at
95 static Node
substr(TNode x
, std::size_t i
, std::size_t j
);
97 /** Return the prefix of x of size at most i */
98 static Node
prefix(TNode x
, std::size_t i
);
100 /** Return the suffix of x of size at most i */
101 static Node
suffix(TNode x
, std::size_t i
);
104 * Checks if there is any overlap between word x and another word y. This
105 * corresponds to checking whether one string contains the other and whether a
106 * substring/subsequence of one is a prefix of the other and vice-versa.
108 * @param x The first string
109 * @param y The second string
110 * @return True if there is an overlap, false otherwise
112 static bool noOverlapWith(TNode x
, TNode y
);
116 * if overlap returns m>0,
117 * then the maximal suffix of this string that is a prefix of y is of length
120 * For example, if x is "abcdef", then:
121 * x.overlap("defg") = 3
122 * x.overlap("ab") = 0
124 * x.overlap("bcdefdef") = 5
126 static std::size_t overlap(TNode x
, TNode y
);
130 * if roverlap returns m>0,
131 * then the maximal prefix of this word that is a suffix of y is of length m.
133 * For example, if x is "abcdef", then:
134 * x.roverlap("aaabc") = 3
135 * x.roverlap("def") = 0
136 * x.roverlap("d") = 0
137 * x.roverlap("defabcde") = 5
139 * Notice that x.overlap(y) = y.roverlap(x)
141 static std::size_t roverlap(TNode x
, TNode y
);
142 /** Return true if word x is a repetition of the same character */
143 static bool isRepeated(TNode x
);
146 * This returns the suffix remainder (resp. prefix remainder when isRev is
147 * true) of words a and b, call it r, such that:
148 * (1) a = b ++ r , or
150 * when isRev = false. The argument index is set to 1 if we are in the second
151 * case, and 0 otherwise.
153 * If a and b do not share a common prefix (resp. suffix), then this method
154 * returns the null node.
156 static Node
splitConstant(TNode x
, TNode y
, size_t& index
, bool isRev
);
159 * Return the result of reversing x.
161 static Node
reverse(TNode x
);
164 // ------------------------------ end for words (string or sequence constants)
166 } // namespace strings
167 } // namespace theory