Updated code slightly. Updated README.md to have examples.

This commit is contained in:
Xnoe 2020-06-08 10:14:07 +01:00 committed by Elsie
parent 7db8e6518e
commit d2d5502163
3 changed files with 42 additions and 3 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
test.cpp
a.out

View File

@ -7,3 +7,39 @@ I expect the performance to be abysmally slow and for it to not really be worth
To use it, just include "utf8.h" or whatever the file name is at the current moment in time. I'll /probably (not)?/ update this in the future. To use it, just include "utf8.h" or whatever the file name is at the current moment in time. I'll /probably (not)?/ update this in the future.
CC0 because I don't deserve credit for how awful this is. CC0 because I don't deserve credit for how awful this is.
Some example code because why not.
Printing a string:
```cpp
#include <iostream>
#include "utf8.h"
int main() {
string32 test = "こんにちは!";
std::cout << test << "\n";
}
```
Getting the third character of a string, without breaking UTF-8 strings.
```cpp
#include <iostream>
#include "utf8.h"
int main() {
string32 test = "こんにちは!";
std::cout << "The third character of `" << test << "` is: " << test[2] << "\n";
}
```
Iterating over a string:
```cpp
#include <iostream>
#include "utf8.h"
int main() {
string32 test = "こんにちは!";
for (int i(0);i<test.size();i++)
std::cout << "Character at index (" << i << ") is: " << test[i] << ".\n";
}
```

7
utf8.h
View File

@ -40,11 +40,12 @@ struct string32 {
while (sd[0]) while (sd[0])
cs.push_back(char32(&sd)); cs.push_back(char32(&sd));
} }
char32 operator[](int i) {return cs[i];} int size() const {return cs.size();}
char32 operator[](int i) const {return cs[i];}
}; };
std::ostream& operator<<(std::ostream& stream, const string32& s32) { std::ostream& operator<<(std::ostream& stream, const string32& s32) {
for (int i=0;i<s32.cs.size();i++) for (int i=0;i<s32.size();i++)
stream << s32.cs[i]; stream << s32[i];
return stream; return stream;
} }