/[debian]/mimetic/trunk/mimetic/utils.h
ViewVC logotype

Contents of /mimetic/trunk/mimetic/utils.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1731 - (show annotations)
Fri May 15 15:35:42 2009 UTC (12 years, 1 month ago) by gregoa
File MIME type: text/plain
File size: 2983 byte(s)
New upstream release.
1 /***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: utils.h,v 1.23 2008-10-07 11:06:26 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #ifndef _MIMETIC_UTILS_H_
17 #define _MIMETIC_UTILS_H_
18 #include <iostream>
19 #include <string>
20 #include <ctype.h>
21 #include <mimetic/libconfig.h>
22 #include <mimetic/strutils.h>
23
24 namespace mimetic
25 {
26
27 std::ostream& crlf(std::ostream&);
28 std::ostream& nl(std::ostream&);
29
30 #ifndef isblank
31 inline int isblank(char c)
32 {
33 return c == ' ' || c == '\t';
34 }
35 #endif
36
37 namespace utils
38 {
39
40 /// returns the filename out of the fqn (fully qualified name)
41 std::string extractFilename(const std::string&);
42
43 /// returns a string representation of \p n
44 std::string int2str(int n);
45
46 /// return true if the string contains just blanks (space and tabs)
47 bool string_is_blank(const std::string&);
48
49 /// returns the integer value represented by \p s
50 int str2int(const std::string& s);
51
52 /// returns a string hexadecimal representation of \p n
53 std::string int2hex(unsigned int n);
54
55 // find_bm specialization for random access iterators
56 template<typename Iterator>
57 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word, const std::random_access_iterator_tag&)
58 {
59 int bLen = word.length();
60 const char* pWord = word.c_str();
61 int i, t, shift[256];
62 unsigned char c;
63
64 for(i = 0; i < 256; ++i)
65 shift[i] = bLen;
66
67 for(i = 0; i < bLen; ++i)
68 shift[ (int) pWord[i] ] = bLen -i - 1;
69
70 for(i = t = bLen-1; t >= 0; --i, --t)
71 {
72 if((bit + i) >= eit)
73 return eit;
74
75 while((c = *(bit + i)) != pWord[t])
76 {
77 i += std::max(bLen-t, shift[c]);
78 if((bit + i) >= eit) return eit;
79 t = bLen-1;
80 }
81 }
82
83 return bit + i + 1;
84 }
85
86 // boyer-moore find
87 /**
88 * find the first occurrence of \p word in (\p bit, \p eit]
89 *
90 * returns an Iterator pointing at the first character of the found pattern
91 * or \p eit if the search fails
92 */
93 template<typename Iterator>
94 Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
95 {
96 return find_bm(bit, eit, word,
97 typename std::iterator_traits<Iterator>::iterator_category());
98 }
99
100
101
102 } // ns utils
103
104 }
105
106 #endif

  ViewVC Help
Powered by ViewVC 1.1.26