~ubuntu-branches/ubuntu/natty/mimetic/natty

« back to all changes in this revision

Viewing changes to mimetic/utils.h

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2006-06-16 13:16:07 UTC
  • Revision ID: james.westby@ubuntu.com-20060616131607-245mqjypkjuahq6b
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    copyright            : (C) 2002-2005 by Stefano Barbato
 
3
    email                : stefano@codesink.org
 
4
 
 
5
    $Id: utils.h,v 1.22 2006/04/10 08:42:44 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
/// returns the integer value represented by \p s
 
47
int str2int(const std::string& s);
 
48
 
 
49
/// returns a string hexadecimal representation of \p n
 
50
std::string int2hex(unsigned int n);
 
51
 
 
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&)
 
55
{
 
56
    int bLen = word.length();
 
57
    const char* pWord = word.c_str();
 
58
    int i, t, shift[256];
 
59
    unsigned char c;
 
60
 
 
61
    for(i = 0; i < 256; ++i)  
 
62
        shift[i] = bLen;
 
63
 
 
64
    for(i = 0; i < bLen; ++i)
 
65
        shift[ (int) pWord[i] ] = bLen -i - 1;
 
66
 
 
67
    for(i = t = bLen-1; t >= 0; --i, --t)
 
68
    {
 
69
        if((bit + i) >= eit)
 
70
            return eit; 
 
71
 
 
72
        while((c = *(bit + i)) != pWord[t]) 
 
73
        {
 
74
            i += std::max(bLen-t, shift[c]);
 
75
            if((bit + i) >= eit) return eit; 
 
76
            t = bLen-1;
 
77
        }
 
78
    }
 
79
 
 
80
    return bit + i + 1;
 
81
}
 
82
 
 
83
// boyer-moore find 
 
84
/**
 
85
 * find the first occurrence of \p word in (\p bit, \p eit]
 
86
 *
 
87
 * returns an Iterator pointing at the first character of the found pattern
 
88
 * or \p eit if the search fails
 
89
 */
 
90
template<typename Iterator>
 
91
Iterator find_bm(Iterator bit, Iterator eit, const std::string& word)
 
92
{
 
93
    return find_bm(bit, eit, word, 
 
94
        typename std::iterator_traits<Iterator>::iterator_category());
 
95
}
 
96
 
 
97
 
 
98
 
 
99
} // ns utils
 
100
 
 
101
}
 
102
 
 
103
#endif