Remove trailing \r when reading line.
[tairon.git] / src / core / string.cpp
blob65ac4c24d6f42b5190804a57ec282b1319ddee23
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation and appearing *
8 * in the file LICENSE.LGPL included in the packaging of this file. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #include <cctype>
18 #include <sstream>
20 #include "string.h"
22 namespace Tairon {
24 namespace Core {
26 String String::null;
28 /* {{{ String::lstrip() */
29 String &String::lstrip()
31 size_t pos = 0;
32 while (isspace((*this)[pos]) && (pos < length()))
33 ++pos;
34 erase(0, pos);
36 return *this;
38 /* }}} */
40 /* {{{ String::number(double) */
41 String String::number(double num)
43 std::stringstream str;
44 str << num;
45 return str.str();
47 /* }}} */
49 /* {{{ String::number(int32_t) */
50 String String::number(int32_t num)
52 std::stringstream str;
53 str << num;
54 return str.str();
56 /* }}} */
58 /* {{{ String::number(uint32_t) */
59 String String::number(uint32_t num)
61 std::stringstream str;
62 str << num;
63 return str.str();
65 /* }}} */
67 /* {{{ String::number(int64_t) */
68 String String::number(int64_t num)
70 std::stringstream str;
71 str << num;
72 return str.str();
74 /* }}} */
76 /* {{{ String::number(uint64_t) */
77 String String::number(uint64_t num)
79 std::stringstream str;
80 str << num;
81 return str.str();
83 /* }}} */
85 /* {{{ String::readLine() */
86 String String::readLine()
88 size_t n = find('\n');
89 if (n == npos)
90 return String();
92 String ret = substr(0, n);
93 if (ret.length() && (ret[ret.length() - 1] == '\r'))
94 ret.resize(ret.length() - 1);
95 erase(0, n + 1);
96 return ret;
98 /* }}} */
100 /* {{{ String::rstrip() */
101 String &String::rstrip()
103 size_t pos = length() - 1;
104 while (isspace((*this)[pos]) && (pos != npos))
105 --pos;
106 erase(pos + 1);
108 return *this;
110 /* }}} */
112 /* {{{ String::strip() */
113 String &String::strip()
115 return lstrip().rstrip();
117 /* }}} */
119 }; // namespace Core
121 }; // namespace Tairon
123 // vim: ai sw=4 ts=4 noet fdm=marker