Skip to content

Commit e5fb460

Browse files
committed
[CPP] Add std::getline(...) and cin>>std::string()
1 parent 69b1c65 commit e5fb460

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

src/usr/include/iostream.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class istream {
1919
public:
2020
istream& operator>>(char &c);
2121
istream& operator>>(char *str);
22-
istream& operator>>(int &num);
22+
istream& operator>>(std::string &str);
23+
24+
istream& get(char &c);
25+
istream& get_line(char *str, std::size_t n, char delim='\n');
2326
};
2427

2528
extern ostream cout;

src/usr/include/string.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class basic_string {
4747
const_iterator begin() const;
4848
const_iterator end() const;
4949

50+
void clear();
5051
void pop_back();
5152
CharT& back();
5253
CharT& front();
@@ -61,6 +62,8 @@ class basic_string {
6162

6263
using string = basic_string<char>;
6364

65+
std::istream& get_line(std::istream &i, std::string &str);
66+
6467
} // namespace std end
6568

6669
#include <string.tcc>

src/usr/include/string.tcc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ typename basic_string<CharT>::const_iterator basic_string<CharT>::begin() const
4141
template <typename CharT>
4242
typename basic_string<CharT>::const_iterator basic_string<CharT>::end() const { return this->_data.end() - 1; }
4343

44+
template <typename CharT>
45+
void basic_string<CharT>::clear() {
46+
this->_data.clear();
47+
this->_data.push_back('\0');
48+
}
49+
4450
template <typename CharT>
4551
void basic_string<CharT>::pop_back() {
4652
if(this->_data.size()>1) {
@@ -101,4 +107,14 @@ basic_string<CharT>& basic_string<CharT>::operator+=(const basic_string<CharT> &
101107
return *this;
102108
}
103109

110+
std::istream& get_line(std::istream &i, std::string &str) {
111+
char ch;
112+
str.clear();
113+
while (1) {
114+
i.get(ch);
115+
if (ch == '\n') break;
116+
str += ch;
117+
}
118+
}
119+
104120
} // namespace std end

src/usr/include/vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class vector {
2222
const_iterator begin() const;
2323
const_iterator end() const;
2424

25-
T *raw_data() const;
25+
void clear();
2626
bool empty();
2727
std::size_t size();
2828
void push_back(const T &val);

src/usr/include/vector.tcc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ void vector<T>::resize_capacity(std::size_t capacity) {
5656
this->_capacity = capacity;
5757
}
5858

59+
template <typename T>
60+
void vector<T>::clear() {
61+
// not reducing capacity for now
62+
this->_size = 0;
63+
}
64+
5965
template <typename T>
6066
bool vector<T>::empty() {
6167
return this->_size == 0;

src/usr/lib/iostream.cpp

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,73 @@
33
#include <conio.h>
44

55
namespace std {
6+
#define __IS_WHITESPACE(ch) ((ch)==' ' || (ch)=='\n' || (ch)=='\t')
67

78
const char endl = '\n';
89
ostream cout;
910
istream cin;
1011

12+
1113
ostream& ostream::operator<<(const char c) { putchar(c); return *this; }
1214
ostream& ostream::operator<<(const int num) { printf("%d", num); return *this; }
1315
ostream& ostream::operator<<(const char *str) { puts(str); return *this; }
1416
ostream& ostream::operator<<(const std::string &str) { puts(str.c_str()); return *this; }
1517

16-
istream& istream::operator>>(char &c) { c = getch(); putchar(c); return *this; }
17-
istream& istream::operator>>(char *str) { gets(str); return *this; }
18+
istream& istream::operator>>(char &c) {
19+
do {
20+
this->get(c);
21+
} while (__IS_WHITESPACE(c));
22+
return *this;
23+
}
24+
25+
istream& istream::operator>>(char *str) {
26+
char c;
27+
bool leading_whitespace = 1;
28+
while(1) {
29+
this->get(c);
30+
if (leading_whitespace) {
31+
if(__IS_WHITESPACE(c)) continue;
32+
leading_whitespace = 0;
33+
} else {
34+
// from second non-whitespace character
35+
if(__IS_WHITESPACE(c)) break;
36+
}
37+
*(str++) = c;
38+
};
39+
*(str++) = '\0';
40+
return *this;
41+
}
42+
43+
istream& istream::operator>>(std::string &str) {
44+
str.clear();
45+
char c;
46+
bool leading_whitespace = 1;
47+
while(1) {
48+
this->get(c);
49+
if (leading_whitespace) {
50+
if(__IS_WHITESPACE(c)) continue;
51+
leading_whitespace = 0;
52+
} else {
53+
// from second non-whitespace character
54+
if(__IS_WHITESPACE(c)) break;
55+
}
56+
str += c;
57+
};
58+
return *this;
59+
}
60+
61+
istream& istream::get(char &c) { c = getch(); putchar(c); return *this; }
62+
63+
istream& istream::get_line(char *str, std::size_t n, char delim) {
64+
char c;
65+
while (n-- > 0) {
66+
this->get(c);
67+
if (c == delim) break;
68+
*(str++) = c;
69+
}
70+
*(str) = '\0';
71+
return *this;
72+
}
1873

74+
#undef __IS_WHITESPACE
1975
} // namespace std end

0 commit comments

Comments
 (0)