单词exp和带空格的字符串

wordexp and strings with spaces

本文关键字:字符串 空格 exp 单词      更新时间:2023-10-16

我正在尝试扩展包含 unix 文件路径的string中的变量。例如,字符串为:

std::string path = "$HOME/Folder  With  Two  Spaces  Next  To  Each  Other".

这是我使用wordexp的代码:

#include <wordexp.h>
#include <string>
#include <iostream>
std::string env_subst(const std::string &path)
{
std::string result = "";
wordexp_t p;
if (!::wordexp(path.c_str(), &p, 0))
{
if (p.we_wordc >= 1)
{
result = std::string(p.we_wordv[0]);
for (uint32_t i = 1; i < p.we_wordc; ++i)
{
result += " " + std::string(p.we_wordv[i]);
}
}
::wordfree(&p);
return result;
}
else
{
// Illegal chars found
return path;
}
}
int main()
{
std::string teststring = "$HOME/Folder  With  Two  Spaces  Next  To  Each  Other";
std::string result = env_subst(teststring);
std::cout << "Result: " << result << std::endl;
return 0;
}

输出为:

Result: /home/nidhoegger/Folder With Two Spaces Next To Each Other

你看,虽然输入中的单词之间有两个空格,但现在只有一个空格。

有没有简单的方法可以解决这个问题?

代码删除路径中的双空格的原因是,对于每个单词后只添加一个空格,而不管实际的空格数如何。此问题的一个可能的解决方案是事先找到路径字符串中的所有空格,然后将它们添加进来。例如,您可以使用如下内容:

std::string spaces[p.we_wordc];
uint32_t pos = path.find(" ", 0);
uint32_t j=0;
while(pos!=std::string::npos){
while(path.at(pos)==' '){
spaces[j]+=" ";
pos++;
}
pos=path.find(" ", pos+1);
j++;
}

使用 std::string::find 遍历路径,并将空格存储在字符串数组中。然后,您可以将 for 循环中的行修改为

result += spaces[i-1] + std::string(p.we_wordv[i]);

以添加适当数量的空格。

如果要在异常命名的文件中保留空格,请将其括在大括号中:std::string teststring = ""~/filename with spaces"";.但是注意原始字符串中有多少空格是没有意义的,因为您必须跳过成对的"并基本上重做wordexp()的功能。在命令中留下多个空格没有多大意义:ls -alls -al完全相同,因此修剪是合理的。OP的代码是完全有效的 - 无需添加任何其他内容。

附言决定将其添加为注释,因为我与OP掉在同一个坑中。