1
/***************************************************************************
2
copyright : (C) 2002-2005 by Stefano Barbato
3
email : stefano@codesink.org
5
$Id: utils.h,v 1.22 2006/04/10 08:42:44 tat Exp $
6
***************************************************************************/
8
/***************************************************************************
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. *
15
***************************************************************************/
16
#ifndef _MIMETIC_UTILS_H_
17
#define _MIMETIC_UTILS_H_
21
#include <mimetic/libconfig.h>
22
#include <mimetic/strutils.h>
27
std::ostream& crlf(std::ostream&);
28
std::ostream& nl(std::ostream&);
31
inline int isblank(char c)
33
return c == ' ' || c == '\t';
40
/// returns the filename out of the fqn (fully qualified name)
41
std::string extractFilename(const std::string&);
43
/// returns a string representation of \p n
44
std::string int2str(int n);
46
/// returns the integer value represented by \p s
47
int str2int(const std::string& s);
49
/// returns a string hexadecimal representation of \p n
50
std::string int2hex(unsigned int n);
52
// find_bm specialization for random access iterators
53
template<typename Iterator>
54
Iterator find_bm(Iterator bit, Iterator eit, const std::string& word, const std::random_access_iterator_tag&)
56
int bLen = word.length();
57
const char* pWord = word.c_str();
61
for(i = 0; i < 256; ++i)
64
for(i = 0; i < bLen; ++i)
65
shift[ (int) pWord[i] ] = bLen -i - 1;
67
for(i = t = bLen-1; t >= 0; --i, --t)
72
while((c = *(bit + i)) != pWord[t])
74
i += std::max(bLen-t, shift[c]);
75
if((bit + i) >= eit) return eit;
85
* find the first occurrence of \p word in (\p bit, \p eit]
87
* returns an Iterator pointing at the first character of the found pattern
88
* or \p eit if the search fails
90
template<typename Iterator>
91
Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
93
return find_bm(bit, eit, word,
94
typename std::iterator_traits<Iterator>::iterator_category());