Commit bcd9dee2f5cddfbbc176138346967f41206bd7ad
- Diff rendering mode:
- inline
- side by side
kernel/lib/string.cc
(43 / 13)
|   | |||
| 16 | 16 | ||
| 17 | 17 | #include "string.h" | |
| 18 | 18 | ||
| 19 | #include "iostream.h" | ||
| 20 | using namespace std; | ||
| 21 | |||
| 19 | 22 | namespace std | |
| 20 | 23 | { | |
| 21 | 24 | // Make an empty string | |
| … | … | ||
| 114 | 114 | return !(* this == s); | |
| 115 | 115 | } | |
| 116 | 116 | ||
| 117 | string string::operator + (const char * s) | ||
| 117 | string operator + (const char * lhs, string& rhs) | ||
| 118 | 118 | { | |
| 119 | char * temp; | ||
| 120 | strncpy(temp, m_data, m_length + 1); | ||
| 119 | // Allocate space to copy the strings | ||
| 120 | char * temp = new char[strlen(lhs) + rhs.length() + 1]; | ||
| 121 | 121 | ||
| 122 | string str; | ||
| 123 | str = strcat(temp, s); | ||
| 122 | // Copy the first string | ||
| 123 | strcpy(temp, lhs); | ||
| 124 | 124 | ||
| 125 | // Copy the second string and make a new string from it | ||
| 126 | string str(strcat(temp, rhs.c_string())); | ||
| 127 | |||
| 128 | // Free the temp string's memory | ||
| 129 | delete [] temp; | ||
| 130 | |||
| 125 | 131 | return str; | |
| 126 | 132 | } | |
| 127 | 133 | ||
| 128 | // TODO: Fix this | ||
| 129 | /*string string::operator + (string s) | ||
| 134 | string operator + (string& lhs, const char * rhs) | ||
| 130 | 135 | { | |
| 131 | char * temp; | ||
| 132 | strcpy(temp, m_data); | ||
| 136 | // Allocate space to copy the strings | ||
| 137 | char * temp = new char[lhs.length() + strlen(rhs) + 1]; | ||
| 133 | 138 | ||
| 134 | string str; | ||
| 135 | str = strcat(temp, s.c_string()); | ||
| 136 | cout << " d"; | ||
| 139 | // Copy the first string | ||
| 140 | strcpy(temp, lhs.c_string()); | ||
| 141 | |||
| 142 | // Copy the second string and make a new string from it | ||
| 143 | string str(strcat(temp, rhs)); | ||
| 144 | |||
| 145 | // Free the temp string's memory | ||
| 146 | delete [] temp; | ||
| 147 | |||
| 137 | 148 | return str; | |
| 138 | }*/ | ||
| 149 | } | ||
| 150 | |||
| 151 | string operator + (string& lhs, string& rhs) | ||
| 152 | { | ||
| 153 | // Allocate space to copy the strings | ||
| 154 | char * temp = new char[lhs.length() + rhs.length() + 1]; | ||
| 155 | |||
| 156 | // Copy the first string | ||
| 157 | strcpy(temp, lhs.c_string()); | ||
| 158 | |||
| 159 | string str(strcat(temp, rhs.c_string())); | ||
| 160 | |||
| 161 | // Free the temporary string's memory | ||
| 162 | delete [] temp; | ||
| 163 | |||
| 164 | return str; | ||
| 165 | } | ||
| 139 | 166 | ||
| 140 | 167 | // Return a C string of the string | |
| 141 | 168 | const char * string::c_string() |
kernel/lib/string.h
(3 / 2)
|   | |||
| 55 | 55 | bool operator != (const char * s); | |
| 56 | 56 | ||
| 57 | 57 | // Addition operator for adding two strings together | |
| 58 | string operator + (const char * s); | ||
| 59 | //string operator + (string s); | ||
| 58 | friend string operator + (const char * lhs, string& rhs); | ||
| 59 | friend string operator + (string& lhs, const char * rhs); | ||
| 60 | friend string operator + (string& lhs, string& rhs); | ||
| 60 | 61 | ||
| 61 | 62 | // Return the length of this string | |
| 62 | 63 | size_t length() const; |

