File: StringTool.cpp

package info (click to toggle)
fillets-ng 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,792 kB
  • ctags: 2,426
  • sloc: cpp: 15,348; sh: 9,781; ansic: 2,363; makefile: 119
file content (122 lines) | stat: -rw-r--r-- 3,087 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
#include "StringTool.h"

#include <stdlib.h>
#include <cstdlib>
#include <sstream>

//-----------------------------------------------------------------
/**
 * Convert string to number.
 * @param strNum string
 * @param ok true on sucess
 * @return number or zero
 */
    long
StringTool::readInt(const char *strNum, bool *ok)
{
    long result = 0;
    *ok = false;

    if (strNum != NULL) {
        char *endptr;
        result = strtol(strNum, &endptr, 0);
        if (strNum[0] != '\0' && endptr[0] == '\0') {
            *ok = true;
        }
    }

    if (!ok) {
        result = 0;
    }

    return result;
}
//-----------------------------------------------------------------
/**
 * Convert long to string
 * @return string value
 */
    std::string
StringTool::toString(long value)
{
    std::ostringstream buffer;
    buffer << value;
    return buffer.str();
}
//-----------------------------------------------------------------
    bool
StringTool::startsWith(const std::string &str,
        const std::string &prefix)
{
    return prefix == str.substr(0, prefix.size());
}
//-----------------------------------------------------------------
/**
 * Replace one substring with another
 * @param buffer string to change
 * @param pattern what replace
 * @param newstring what to place
 */
    void
StringTool::replace(std::string &buffer,
        const std::string &pattern, const std::string &newstring)
{
    std::string::size_type pos = buffer.find(pattern);
    while (pos != std::string::npos) {
        buffer.replace(pos, pattern.size(), newstring);
        pos = buffer.find(pattern, pos + newstring.size());
    }
}
//-----------------------------------------------------------------
/**
 * Split string.
 * @return splited segments without separator
 */
    StringTool::t_args
StringTool::split(const std::string &str, char separator)
{
    std::string remain = str;
    t_args args;

    std::string::size_type pos = remain.find(separator);
    while (pos != std::string::npos) {
        args.push_back(remain.substr(0, pos));
        remain.erase(0, pos + 1);

        pos = remain.find(separator);
    }
    if (remain.size() > 0) {
        args.push_back(remain);
    }

    return args;
}

//-----------------------------------------------------------------
/**
 * Return number of Unicode entities inside given UTF-8 text.
 */
    int
StringTool::utf8Length(const std::string &str) {
    int length = 0;
    const char *p = str.c_str();
    char c;
    // How to recognize entity:
    // Unicode entities start with 0xxxxxxx or 11xxxxxx,
    // the other bytes start with 10xxxxxx.
    // (thanks to Egmont Koblinger)
    while ((c = *p++) != 0) {
        if ((c & 0xC0) != 0x80) {
            length++;
        }
    }
    return length;
}