C++ : Interpret unicode white space
I have a file which contains text (ASCII + unicode) and I am trying to count total words in it using a C++ program. It is a requirement that I should read the file line by line (using getline) and then process each line to count the words within it.
So I have written the following simple program:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
++ct;
}
}
std::cout << ct << std::endl;
return 0;
}
However, the above program outputs a number that is lesser than what wc -w command gives. To narrow down the problem, I modified the program to simply output whatever it reads. So now the program becomes:
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
std::cout << token << " ";
}
std::cout << std::endl;
}
return 0;
}
I redirected the output of this program to another file. Now, when I run wc -w on this new file, the number is same as running wc -w on the original file. This means, I am reading all the words (i.e., "words" defined by wc) in my program. And hence, a reasonable explanation would be that one of the values of token that is read using inputStream >> token consists of some unicode character that is interpreted as a white space by wc program. So how do I change my program to also support such interpretation of unicode white space characters?
c++ string unicode
|
show 2 more comments
I have a file which contains text (ASCII + unicode) and I am trying to count total words in it using a C++ program. It is a requirement that I should read the file line by line (using getline) and then process each line to count the words within it.
So I have written the following simple program:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
++ct;
}
}
std::cout << ct << std::endl;
return 0;
}
However, the above program outputs a number that is lesser than what wc -w command gives. To narrow down the problem, I modified the program to simply output whatever it reads. So now the program becomes:
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
std::cout << token << " ";
}
std::cout << std::endl;
}
return 0;
}
I redirected the output of this program to another file. Now, when I run wc -w on this new file, the number is same as running wc -w on the original file. This means, I am reading all the words (i.e., "words" defined by wc) in my program. And hence, a reasonable explanation would be that one of the values of token that is read using inputStream >> token consists of some unicode character that is interpreted as a white space by wc program. So how do I change my program to also support such interpretation of unicode white space characters?
c++ string unicode
1
That's not going to be easy without external dependencies. Boost implements this as a lookup table to map each character to a given type, and then checks the type with some heuristics. Basically, you either need decent Unicode support, like Boost or Qt, or don't bother. boost.org/doc/libs/1_62_0/boost/spirit/home/support/…
– Alexander Huszagh
Jun 28 '17 at 23:30
If you are interested, the relevant task would be to convert your Unicode text to UTF-32 (check if a start byte or a continuation byte, or plain ASCII), check the code point to see if it is whitespace using a lookup table, and then use it as a token. If that sounds involved, and a lot of work, it is, and there are many libraries that already do this for you. Boost notably is one.
– Alexander Huszagh
Jun 28 '17 at 23:34
2
There are quite a few "Space" characters in Unicode - so this becomes a map of characters and lookups. Error prone to because of the need to test against all the fun that can come through. I will say, from a high level, that C++ may not be the best language for string manipulations like this.
– Michael Dorgan
Jun 28 '17 at 23:36
@AlexanderHuszagh: I see. I can use boost for this task. What would be the best way to tokenize a string using boost? Doing that would be easier than usingisspacecheck for each character.
– John Elaine
Jun 28 '17 at 23:53
@JohnElaine What's your source encoding, and can you convert to UTF-32? Checking for whitespace is easiest in UTF-32. Basically, I would get line-by-line, convert to UTF-32, iterate code point by code point and check if each is whitespace and process token-by-token. It's not pretty, but it can be pretty easy to do. If you do not want to convert to UTF-32, you could look character by character in UTF-8: if it's an ASCII character (< 128), check against known ASCII space characters, otherwise, fill to a uint32_t.
– Alexander Huszagh
Jun 29 '17 at 0:03
|
show 2 more comments
I have a file which contains text (ASCII + unicode) and I am trying to count total words in it using a C++ program. It is a requirement that I should read the file line by line (using getline) and then process each line to count the words within it.
So I have written the following simple program:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
++ct;
}
}
std::cout << ct << std::endl;
return 0;
}
However, the above program outputs a number that is lesser than what wc -w command gives. To narrow down the problem, I modified the program to simply output whatever it reads. So now the program becomes:
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
std::cout << token << " ";
}
std::cout << std::endl;
}
return 0;
}
I redirected the output of this program to another file. Now, when I run wc -w on this new file, the number is same as running wc -w on the original file. This means, I am reading all the words (i.e., "words" defined by wc) in my program. And hence, a reasonable explanation would be that one of the values of token that is read using inputStream >> token consists of some unicode character that is interpreted as a white space by wc program. So how do I change my program to also support such interpretation of unicode white space characters?
c++ string unicode
I have a file which contains text (ASCII + unicode) and I am trying to count total words in it using a C++ program. It is a requirement that I should read the file line by line (using getline) and then process each line to count the words within it.
So I have written the following simple program:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
++ct;
}
}
std::cout << ct << std::endl;
return 0;
}
However, the above program outputs a number that is lesser than what wc -w command gives. To narrow down the problem, I modified the program to simply output whatever it reads. So now the program becomes:
int main(int argc, char* argv) {
uint64_t ct = 0;
std::string line;
std::ifstream infile(argv[1]);
while(std::getline(infile, line)) {
std::stringstream inputStream(line);
std::string token;
while (inputStream >> token) {
std::cout << token << " ";
}
std::cout << std::endl;
}
return 0;
}
I redirected the output of this program to another file. Now, when I run wc -w on this new file, the number is same as running wc -w on the original file. This means, I am reading all the words (i.e., "words" defined by wc) in my program. And hence, a reasonable explanation would be that one of the values of token that is read using inputStream >> token consists of some unicode character that is interpreted as a white space by wc program. So how do I change my program to also support such interpretation of unicode white space characters?
c++ string unicode
c++ string unicode
asked Jun 28 '17 at 23:27
John ElaineJohn Elaine
12413
12413
1
That's not going to be easy without external dependencies. Boost implements this as a lookup table to map each character to a given type, and then checks the type with some heuristics. Basically, you either need decent Unicode support, like Boost or Qt, or don't bother. boost.org/doc/libs/1_62_0/boost/spirit/home/support/…
– Alexander Huszagh
Jun 28 '17 at 23:30
If you are interested, the relevant task would be to convert your Unicode text to UTF-32 (check if a start byte or a continuation byte, or plain ASCII), check the code point to see if it is whitespace using a lookup table, and then use it as a token. If that sounds involved, and a lot of work, it is, and there are many libraries that already do this for you. Boost notably is one.
– Alexander Huszagh
Jun 28 '17 at 23:34
2
There are quite a few "Space" characters in Unicode - so this becomes a map of characters and lookups. Error prone to because of the need to test against all the fun that can come through. I will say, from a high level, that C++ may not be the best language for string manipulations like this.
– Michael Dorgan
Jun 28 '17 at 23:36
@AlexanderHuszagh: I see. I can use boost for this task. What would be the best way to tokenize a string using boost? Doing that would be easier than usingisspacecheck for each character.
– John Elaine
Jun 28 '17 at 23:53
@JohnElaine What's your source encoding, and can you convert to UTF-32? Checking for whitespace is easiest in UTF-32. Basically, I would get line-by-line, convert to UTF-32, iterate code point by code point and check if each is whitespace and process token-by-token. It's not pretty, but it can be pretty easy to do. If you do not want to convert to UTF-32, you could look character by character in UTF-8: if it's an ASCII character (< 128), check against known ASCII space characters, otherwise, fill to a uint32_t.
– Alexander Huszagh
Jun 29 '17 at 0:03
|
show 2 more comments
1
That's not going to be easy without external dependencies. Boost implements this as a lookup table to map each character to a given type, and then checks the type with some heuristics. Basically, you either need decent Unicode support, like Boost or Qt, or don't bother. boost.org/doc/libs/1_62_0/boost/spirit/home/support/…
– Alexander Huszagh
Jun 28 '17 at 23:30
If you are interested, the relevant task would be to convert your Unicode text to UTF-32 (check if a start byte or a continuation byte, or plain ASCII), check the code point to see if it is whitespace using a lookup table, and then use it as a token. If that sounds involved, and a lot of work, it is, and there are many libraries that already do this for you. Boost notably is one.
– Alexander Huszagh
Jun 28 '17 at 23:34
2
There are quite a few "Space" characters in Unicode - so this becomes a map of characters and lookups. Error prone to because of the need to test against all the fun that can come through. I will say, from a high level, that C++ may not be the best language for string manipulations like this.
– Michael Dorgan
Jun 28 '17 at 23:36
@AlexanderHuszagh: I see. I can use boost for this task. What would be the best way to tokenize a string using boost? Doing that would be easier than usingisspacecheck for each character.
– John Elaine
Jun 28 '17 at 23:53
@JohnElaine What's your source encoding, and can you convert to UTF-32? Checking for whitespace is easiest in UTF-32. Basically, I would get line-by-line, convert to UTF-32, iterate code point by code point and check if each is whitespace and process token-by-token. It's not pretty, but it can be pretty easy to do. If you do not want to convert to UTF-32, you could look character by character in UTF-8: if it's an ASCII character (< 128), check against known ASCII space characters, otherwise, fill to a uint32_t.
– Alexander Huszagh
Jun 29 '17 at 0:03
1
1
That's not going to be easy without external dependencies. Boost implements this as a lookup table to map each character to a given type, and then checks the type with some heuristics. Basically, you either need decent Unicode support, like Boost or Qt, or don't bother. boost.org/doc/libs/1_62_0/boost/spirit/home/support/…
– Alexander Huszagh
Jun 28 '17 at 23:30
That's not going to be easy without external dependencies. Boost implements this as a lookup table to map each character to a given type, and then checks the type with some heuristics. Basically, you either need decent Unicode support, like Boost or Qt, or don't bother. boost.org/doc/libs/1_62_0/boost/spirit/home/support/…
– Alexander Huszagh
Jun 28 '17 at 23:30
If you are interested, the relevant task would be to convert your Unicode text to UTF-32 (check if a start byte or a continuation byte, or plain ASCII), check the code point to see if it is whitespace using a lookup table, and then use it as a token. If that sounds involved, and a lot of work, it is, and there are many libraries that already do this for you. Boost notably is one.
– Alexander Huszagh
Jun 28 '17 at 23:34
If you are interested, the relevant task would be to convert your Unicode text to UTF-32 (check if a start byte or a continuation byte, or plain ASCII), check the code point to see if it is whitespace using a lookup table, and then use it as a token. If that sounds involved, and a lot of work, it is, and there are many libraries that already do this for you. Boost notably is one.
– Alexander Huszagh
Jun 28 '17 at 23:34
2
2
There are quite a few "Space" characters in Unicode - so this becomes a map of characters and lookups. Error prone to because of the need to test against all the fun that can come through. I will say, from a high level, that C++ may not be the best language for string manipulations like this.
– Michael Dorgan
Jun 28 '17 at 23:36
There are quite a few "Space" characters in Unicode - so this becomes a map of characters and lookups. Error prone to because of the need to test against all the fun that can come through. I will say, from a high level, that C++ may not be the best language for string manipulations like this.
– Michael Dorgan
Jun 28 '17 at 23:36
@AlexanderHuszagh: I see. I can use boost for this task. What would be the best way to tokenize a string using boost? Doing that would be easier than using
isspace check for each character.– John Elaine
Jun 28 '17 at 23:53
@AlexanderHuszagh: I see. I can use boost for this task. What would be the best way to tokenize a string using boost? Doing that would be easier than using
isspace check for each character.– John Elaine
Jun 28 '17 at 23:53
@JohnElaine What's your source encoding, and can you convert to UTF-32? Checking for whitespace is easiest in UTF-32. Basically, I would get line-by-line, convert to UTF-32, iterate code point by code point and check if each is whitespace and process token-by-token. It's not pretty, but it can be pretty easy to do. If you do not want to convert to UTF-32, you could look character by character in UTF-8: if it's an ASCII character (< 128), check against known ASCII space characters, otherwise, fill to a uint32_t.
– Alexander Huszagh
Jun 29 '17 at 0:03
@JohnElaine What's your source encoding, and can you convert to UTF-32? Checking for whitespace is easiest in UTF-32. Basically, I would get line-by-line, convert to UTF-32, iterate code point by code point and check if each is whitespace and process token-by-token. It's not pretty, but it can be pretty easy to do. If you do not want to convert to UTF-32, you could look character by character in UTF-8: if it's an ASCII character (< 128), check against known ASCII space characters, otherwise, fill to a uint32_t.
– Alexander Huszagh
Jun 29 '17 at 0:03
|
show 2 more comments
1 Answer
1
active
oldest
votes
You can go by either:
A. Java's definition of Unicode (not non-breaking) whitespace.
or
B. Wikipedia's list of all 25 Unicode code points defined as whitespace.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44814073%2fc-interpret-unicode-white-space%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can go by either:
A. Java's definition of Unicode (not non-breaking) whitespace.
or
B. Wikipedia's list of all 25 Unicode code points defined as whitespace.
add a comment |
You can go by either:
A. Java's definition of Unicode (not non-breaking) whitespace.
or
B. Wikipedia's list of all 25 Unicode code points defined as whitespace.
add a comment |
You can go by either:
A. Java's definition of Unicode (not non-breaking) whitespace.
or
B. Wikipedia's list of all 25 Unicode code points defined as whitespace.
You can go by either:
A. Java's definition of Unicode (not non-breaking) whitespace.
or
B. Wikipedia's list of all 25 Unicode code points defined as whitespace.
answered Nov 26 '18 at 19:49
veganaiZeveganaiZe
313112
313112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44814073%2fc-interpret-unicode-white-space%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That's not going to be easy without external dependencies. Boost implements this as a lookup table to map each character to a given type, and then checks the type with some heuristics. Basically, you either need decent Unicode support, like Boost or Qt, or don't bother. boost.org/doc/libs/1_62_0/boost/spirit/home/support/…
– Alexander Huszagh
Jun 28 '17 at 23:30
If you are interested, the relevant task would be to convert your Unicode text to UTF-32 (check if a start byte or a continuation byte, or plain ASCII), check the code point to see if it is whitespace using a lookup table, and then use it as a token. If that sounds involved, and a lot of work, it is, and there are many libraries that already do this for you. Boost notably is one.
– Alexander Huszagh
Jun 28 '17 at 23:34
2
There are quite a few "Space" characters in Unicode - so this becomes a map of characters and lookups. Error prone to because of the need to test against all the fun that can come through. I will say, from a high level, that C++ may not be the best language for string manipulations like this.
– Michael Dorgan
Jun 28 '17 at 23:36
@AlexanderHuszagh: I see. I can use boost for this task. What would be the best way to tokenize a string using boost? Doing that would be easier than using
isspacecheck for each character.– John Elaine
Jun 28 '17 at 23:53
@JohnElaine What's your source encoding, and can you convert to UTF-32? Checking for whitespace is easiest in UTF-32. Basically, I would get line-by-line, convert to UTF-32, iterate code point by code point and check if each is whitespace and process token-by-token. It's not pretty, but it can be pretty easy to do. If you do not want to convert to UTF-32, you could look character by character in UTF-8: if it's an ASCII character (< 128), check against known ASCII space characters, otherwise, fill to a uint32_t.
– Alexander Huszagh
Jun 29 '17 at 0:03