Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
string_example.cc
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 int main()
41 {
42  EST_String example("hello world");
43  EST_Regex exclamation("\\(wow\\|yey\\|lo\\)[^a-z]");
44 
45  cout << example << "\n";
46  cout.flush();
47  printf("stdio version %s\n", (const char *)example);
48  fflush(stdout);
49 
50  if (example.contains(exclamation))
51  cout << "\nYes, it contains a match for " << exclamation << "\n";
52 
53  // find a match and extract the thing in brackets
54  int start_br[EST_Regex_max_subexpressions];
55  int end_br[EST_Regex_max_subexpressions];
56  int len;
57 
58  if (example.search(exclamation, len, 0, start_br, end_br)>=0)
59  {
60  // whole match is item 0
61  cout << "match was '" << example.at(start_br[0], end_br[0]- start_br[0]) << "'\n";
62 
63  // first set of brackets.
64  cout << " word was '" << example.at(start_br[1], end_br[1]- start_br[1]) << "'\n";
65  }
66 
67  cout << "\n";
68 
69  // substituting into a string based on a regular expression
70  EST_String source("http://www.cstr.ed.ac.uk/speech_tools");
71  EST_Regex url_re("\\([a-z]*\\)://\\([^/]*\\)\\(.*\\)");
72  EST_String target("protocol=\\1 host=\\2 path=\\3 dummy=\\6");
73 
74  cout << "processing '" <<source <<"'\n";
75  if (source.matches(url_re,
76  0,
77  start_br, end_br))
78  {
79  target.subst(source, start_br, end_br);
80  cout << " gives '" << target <<"'\n";
81  }
82  else
83  cout <<"No match for URL RE\n";
84 
85  EST_String complex("what if I don't like 'hello world'?");
86 
87  EST_String quoted(complex.quote_if_needed('\''));
88  EST_String unquoted(quoted.unquote_if_needed('\''));
89 
90  cout << "\n";
91  cout << "start with \"" << complex << "\"\n";
92  cout << " quoted \"" << quoted << "\"\n";
93  cout << " unquoted \"" << unquoted << "\"\n";
94 
95 #if 0
96  EST_String gubbins = unquoted + quoted;
97 
98  cout << " gubbins \"" << gubbins << "\"\n";
99 #endif
100 
101  return 0;
102 }