C ++字符串

编程入门 行业动态 更新时间:2024-10-28 04:18:53
C ++字符串 - 如何为wstring实现这个'IsEmptyOrSpace(string)'方法?(C++ Strings - How can I implement this 'IsEmptyOrSpace(string)' method for wstring's?)

好的,我在StackOverflow上搜索了如何检查字符串是空还是只有空格。 但是,它只适用于ANSI字符串。 如何让它与wstring一起使用?

这是代码:

#include <string> using namespace std; //! Checks if a string is empty or is whitespace. bool IsEmptyOrSpace(const string& str) { string::const_iterator it = str.begin(); do { if (it == str.end()) return true; } while (*it >= 0 && *it <= 0x7f && isspace(*(it++))); // One of these conditions will be optimized away by the compiler. // Which one depends on whether the characters are signed or not. return false; }

我的第一个想法是将isspace(*(it++))更改为iswspace(*(it++)) ,但之前的两个条件只适用于ASCII,对吧? 这是我到目前为止尝试将函数调整为wstring的:

bool IsEmptyOrSpaceW(const wstring& str) { String::const_iterator it = str.begin(); do { if (it == str.end()) return true; } while (*it >= 0 && *it <= 0x7f && iswspace(*(it++))); // One of these conditions will be optimized away by the compiler. // Which one depends on whether the characters are signed or not. // Do I need to change "*it >= 0 && *it <= 0x7f" to something else? return false; }

我的方法接近正确吗? 无论哪种方式,我如何实现此IsEmptyOrSpace()函数的Unicode版本?

编辑:好的,如果你需要知道为什么*it >= 0 && *it <= 0x7f测试就在那里,我不能告诉你,因为我不知道。 我从这个问题的答案得到了函数的代码: C ++检查字符串是空格还是空所以让我从头开始,一般来说,我可以检查一下wstring是EMPTY还是只是空格?

Okay, I searched StackOverflow on how to check if a string is empty or just whitespace. But, it only works with ANSI strings. How can I get it to work with a wstring?

Here is the code:

#include <string> using namespace std; //! Checks if a string is empty or is whitespace. bool IsEmptyOrSpace(const string& str) { string::const_iterator it = str.begin(); do { if (it == str.end()) return true; } while (*it >= 0 && *it <= 0x7f && isspace(*(it++))); // One of these conditions will be optimized away by the compiler. // Which one depends on whether the characters are signed or not. return false; }

My first thought was to change isspace(*(it++)) to iswspace(*(it++)), but the two conditions before that will only work with ASCII, right? Here is what I have so far on attempting to adapt the function to wstring's:

bool IsEmptyOrSpaceW(const wstring& str) { String::const_iterator it = str.begin(); do { if (it == str.end()) return true; } while (*it >= 0 && *it <= 0x7f && iswspace(*(it++))); // One of these conditions will be optimized away by the compiler. // Which one depends on whether the characters are signed or not. // Do I need to change "*it >= 0 && *it <= 0x7f" to something else? return false; }

Is my approach close to being correct? Either way, how can I implement a Unicode version of this IsEmptyOrSpace() function?

EDIT: Okay, if you need to know why the *it >= 0 && *it <= 0x7f test is there, I cannot tell you, because I do not know. I got the code for the function from the answer to this question: C++ check if string is space or null So let me start from scratch, how, in general, may I check if a wstring is EMPTY or just whitespace?

最满意答案

但之前的两个条件只适用于ASCII,对吧?

那就对了。 它们确保该值符合isspace的前提条件:参数“必须具有unsigned char或EOF的值”。 严格地说,你只需要*it >= 0检查,如果char是无符号的,应该优化掉; 或者,如评论中所述,您可以将值转换为unsigned 。

iswspace没有这样的前提条件,所以只需从宽版本中删除这些检查:

bool IsEmptyOrSpaceW(const wstring& str) { wstring::const_iterator it = str.begin(); do { if (it == str.end()) return true; } while (iswspace(*(it++))); return false; }

作为一种风格,没有必要添加像W这样的奇怪的疣来表示参数类型,因为你可以用不同的参数类型重载IsEmptyOrSpace 。

but the two conditions before that will only work with ASCII, right?

That's right. They make sure the value conforms to the precondition of isspace: the argument "must have the value of an unsigned char or EOF". Strictly speaking, you only need the *it >= 0 check, which should be optimised out if char is unsigned; alternatively, as mentioned in the comments, you could convert the value to unsigned.

iswspace has no such precondition, so just remove those checks from the wide version:

bool IsEmptyOrSpaceW(const wstring& str) { wstring::const_iterator it = str.begin(); do { if (it == str.end()) return true; } while (iswspace(*(it++))); return false; }

As a matter of style, there's no need to add a strange wart like W to indicate the parameter type, since you can just overload IsEmptyOrSpace with different parameter types.

更多推荐

本文发布于:2023-08-07 12:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!