-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyString.h
More file actions
63 lines (48 loc) · 1.9 KB
/
Copy pathMyString.h
File metadata and controls
63 lines (48 loc) · 1.9 KB
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
#pragma once
#include <iostream>
class MyString {
private:
char* data_; // pointer to string (with terminating '\0')
size_t size_; // length of the string (without null terminator)
// Assigns new content (copies string)
void assign(const char* str);
// Creates a new buffer of size len1 + len2 + 1 and copies s1 + s2
static char* concat(const char* s1, size_t len1, const char* s2, size_t len2);
public:
static const size_t npos = static_cast<size_t>(-1);
// Constructors
MyString();
MyString(const char* str);
MyString(const MyString& other);
// Destructor
~MyString();
// Copy assignment
MyString& operator=(const MyString& other);
// Move
MyString(MyString&& other) noexcept;
MyString& operator=(MyString&& other) noexcept;
// Access methods
const char* c_str() const;
bool empty() const;
size_t size() const;
// Search
size_t find_last_of(const char* chars) const;
size_t find_last_of(char c) const;
// Substring
MyString substr(size_t pos = 0, size_t count = npos) const;
// Comparison operators
friend bool operator==(const MyString& lhs, const MyString& rhs);
friend bool operator==(const MyString& lhs, const char* rhs);
friend bool operator==(const char* lhs, const MyString& rhs);
friend bool operator<(const MyString& lhs, const MyString& rhs);
// Concatenation assignment
MyString& operator+=(const char* rhs);
MyString& operator+=(const MyString& rhs);
// Concatenation
friend MyString operator+(const MyString& lhs, const char* rhs);
friend MyString operator+(const MyString& lhs, const MyString& rhs);
// Input/Output
friend std::ostream& operator<<(std::ostream& os, const MyString& str);
friend std::istream& operator>>(std::istream& is, MyString& str);
friend std::istream& getline(std::istream& is, MyString& str, char delim = '\n');
};