Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
utils.h
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#ifndef _MIMETIC_UTILS_H_
8#define _MIMETIC_UTILS_H_
9#include <iostream>
10#include <string>
11#include <ctype.h>
12#include <mimetic/libconfig.h>
13#include <mimetic/strutils.h>
14
15namespace mimetic
16{
17
18std::ostream& crlf(std::ostream&);
19std::ostream& nl(std::ostream&);
20
21#ifndef isblank
22inline int isblank(char c)
23{
24 return c == ' ' || c == '\t';
25}
26#endif
27
28namespace utils
29{
30
31/// returns the filename out of the fqn (fully qualified name)
32std::string extractFilename(const std::string&);
33
34/// returns a string representation of \p n
35std::string int2str(int n);
36
37/// return true if the string contains just blanks (space and tabs)
38bool string_is_blank(const std::string&);
39
40/// returns the integer value represented by \p s
41int str2int(const std::string& s);
42
43/// returns a string hexadecimal representation of \p n
44std::string int2hex(unsigned int n);
45
46// find_bm specialization for random access iterators
47template<typename Iterator>
48Iterator find_bm(Iterator bit, Iterator eit, const std::string& word, const std::random_access_iterator_tag&)
49{
50 int bLen = word.length();
51 const char* pWord = word.c_str();
52 int i, t, shift[256];
53 unsigned char c;
54
55 for(i = 0; i < 256; ++i)
56 shift[i] = bLen;
57
58 for(i = 0; i < bLen; ++i)
59 shift[ (unsigned char) pWord[i] ] = bLen -i - 1;
60
61 for(i = t = bLen-1; t >= 0; --i, --t)
62 {
63 if((bit + i) >= eit)
64 return eit;
65
66 while((c = *(bit + i)) != pWord[t])
67 {
68 i += std::max(bLen-t, shift[c]);
69 if((bit + i) >= eit) return eit;
70 t = bLen-1;
71 }
72 }
73
74 return bit + i + 1;
75}
76
77// boyer-moore find
78/**
79 * find the first occurrence of \p word in (\p bit, \p eit]
80 *
81 * returns an Iterator pointing at the first character of the found pattern
82 * or \p eit if the search fails
83 */
84template<typename Iterator>
85Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
86{
87 return find_bm(bit, eit, word,
88 typename std::iterator_traits<Iterator>::iterator_category());
89}
90
91
92
93} // ns utils
94
95}
96
97#endif
Definition body.h:18