Edinburgh Speech Tools  2.1-release
string_example.cc
Go to the documentation of this file.
1 /************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996,1997 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /************************************************************************/
33 /* Author: Richard Caley */
34 /* Date: May 1997 */
35 /************************************************************************/
36 #include "EST_String.h"
37 #include <iostream>
38 #include <cstdio>
39 
40 using namespace std;
41 
42 int main()
43 {
44  EST_String example("hello world");
45  EST_Regex exclamation("\\(wow\\|yey\\|lo\\)[^a-z]");
46 
47  cout << example << "\n";
48  cout.flush();
49  printf("stdio version %s\n", (const char *)example);
50  fflush(stdout);
51 
52  if (example.contains(exclamation))
53  cout << "\nYes, it contains a match for " << exclamation << "\n";
54 
55  // find a match and extract the thing in brackets
56  size_t start_br[EST_Regex_max_subexpressions];
57  size_t end_br[EST_Regex_max_subexpressions];
58  size_t len;
59 
60  if (example.search(exclamation, len, 0, start_br, end_br)!=EST_STRING_ERR_IDX)
61  {
62  // whole match is item 0
63  cout << "match was '" << example.at(start_br[0], end_br[0]- start_br[0]) << "'\n";
64 
65  // first set of brackets.
66  cout << " word was '" << example.at(start_br[1], end_br[1]- start_br[1]) << "'\n";
67  }
68 
69  cout << "\n";
70 
71  // substituting into a string based on a regular expression
72  EST_String source("http://www.cstr.ed.ac.uk/speech_tools");
73  EST_Regex url_re("\\([a-z]*\\)://\\([^/]*\\)\\(.*\\)");
74  EST_String target("protocol=\\1 host=\\2 path=\\3 dummy=\\6");
75 
76  cout << "processing '" <<source <<"'\n";
77  if (source.matches(url_re,
78  0,
79  start_br, end_br))
80  {
81  target.subst(source, start_br, end_br);
82  cout << " gives '" << target <<"'\n";
83  }
84  else
85  cout <<"No match for URL RE\n";
86 
87  EST_String complex("what if I don't like 'hello world'?");
88 
89  EST_String quoted(complex.quote_if_needed('\''));
90  EST_String unquoted(quoted.unquote_if_needed('\''));
91 
92  cout << "\n";
93  cout << "start with \"" << complex << "\"\n";
94  cout << " quoted \"" << quoted << "\"\n";
95  cout << " unquoted \"" << unquoted << "\"\n";
96 
97 #if 0
98  EST_String gubbins = unquoted + quoted;
99 
100  cout << " gubbins \"" << gubbins << "\"\n";
101 #endif
102 
103  return 0;
104 }
int subst(EST_String source, size_t(&starts)[EST_Regex_max_subexpressions], size_t(&ends)[EST_Regex_max_subexpressions])
Substitute the result of a match into a string.
Definition: EST_String.cc:465
InputSource source
Definition: rxp.c:24
EST_String quote_if_needed(const char quotec) const
Return in quotes if there is something to protect (e.g. spaces)
Definition: EST_String.cc:1056
A Regular expression class to go with the CSTR EST_String class.
Definition: EST_Regex.h:56
#define EST_Regex_max_subexpressions
Definition: EST_Regex.h:150
int main()
#define EST_STRING_ERR_IDX
Definition: EST_String.h:116
EST_String at(int from, int len=0) const
Return part at position.
Definition: EST_String.h:292