Last active
December 30, 2015 19:09
-
-
Save kinnou02/7872124 to your computer and use it in GitHub Desktop.
multibyte compliant substr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string substr(const std::string& str, size_t nb){ | |
std::stringstream result; | |
auto it = str.begin(); | |
unsigned char c; | |
size_t count = 0; | |
while(it != str.end() && count < nb){ | |
c = *it; | |
result << c; | |
if(c >= 0xc0){ | |
++it; | |
c = *it; | |
while((c & 0xc0) == 0x80 && it != str.end()){ | |
result << c; | |
++it; | |
c = *it; | |
} | |
}else{ | |
++it; | |
} | |
++count; | |
} | |
return result.str(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment