Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一个单词的快速填充strcpy 我试图编写一个非常便宜的C++代码片段,对一个空的空字符串进行以下操作。_C++_C_Word_Memcpy_Strcpy - Fatal编程技术网

一个单词的快速填充strcpy 我试图编写一个非常便宜的C++代码片段,对一个空的空字符串进行以下操作。

一个单词的快速填充strcpy 我试图编写一个非常便宜的C++代码片段,对一个空的空字符串进行以下操作。,c++,c,word,memcpy,strcpy,C++,C,Word,Memcpy,Strcpy,输入是类似于“ABC”的字符串。它以null结尾,最大长度为4(或以null结尾的长度为5) 输出转到一个char[4],该字符不是以null结尾的,应该在右侧填充空格。所以在这种情况下,它将是{'A','B','C','} 假设输入字符串正确地以null结尾是可以的,因此不需要读取输入的第二个字来确保。4字节是最长的 因此,它周围的代码如下所示: char* input = "AB"; char output[4]; // code snippet goes here // afterward

输入是类似于
“ABC”
的字符串。它以null结尾,最大长度为4(或以null结尾的长度为5)

输出转到一个
char[4]
,该字符不是以null结尾的,应该在右侧填充空格。所以在这种情况下,它将是
{'A','B','C','}

假设输入字符串正确地以null结尾是可以的,因此不需要读取输入的第二个字来确保。4字节是最长的

因此,它周围的代码如下所示:

char* input = "AB";
char output[4];
// code snippet goes here
// afterward output will be populated with {'A','B',' ',' '}
typedef unsigned int word;
int spacePad(word input) {
    static const word spaces = 0x20202020;

    word mask =
       !input ?                0 :
       !(input & 0x00ffffff) ? 0xff:
       !(input & 0x0000ffff) ? 0xffff :
       !(input & 0x0000ff)   ? 0xffffff :
                               0xffffffff;
    // or without branches
    word branchless_mask =
       1u << (8 * (
         bool(input & 0xff000000) +
         bool(input & 0x00ff0000) +
         bool(input & 0x0000ff00) +
         bool(input & 0x000000ff)
       ));

    return (spaces & mask) | (input & ~mask);
}
这样做的成本有多低? 如果有关系:我正在与:

Linux 2.6.32-358.11.1.el6.x86_64#1 SMP x86_64 x86_64 x86_64 GNU/Linux


最后,输入是字对齐的。

如果您的输入以null结尾,一个简单的strcpy就足够了。memcpy速度更快,但会复制在空字符后找到的任何垃圾

如果您的输入以null结尾,一个简单的
strcpy
就足够了。memcpy速度更快,但会复制在空字符后找到的任何垃圾

您正在寻找
memcpy

char* input = "AB\0\0";
char output[4];
memcpy(output, input, 4);
如果输入是可变的,则需要首先计算大小:

char* input = "AB";
std::size_t len = strlen(input);
char output[4] = {' ', ' ', ' ', ' '};
memcpy(output, input, std::min(4, len));

您正在寻找
memcpy

char* input = "AB\0\0";
char output[4];
memcpy(output, input, 4);
如果输入是可变的,则需要首先计算大小:

char* input = "AB";
std::size_t len = strlen(input);
char output[4] = {' ', ' ', ' ', ' '};
memcpy(output, input, std::min(4, len));

如果速度是你的问题-使用暴力

这不会访问其边界之外的
输入
,也不会破坏它

 const char* input = TBD();
 char output[4] = {' '};
 if (input[0]) {
   output[0] = input[0];
   if (input[1]) {
     output[1] = input[1];
     if (input[2]) {
       output[2] = input[2];
       if (input[3]) {
         output[3] = input[3];
       }
     }
   }
 }

如果速度是你的问题-使用暴力

这不会访问其边界之外的
输入
,也不会破坏它

 const char* input = TBD();
 char output[4] = {' '};
 if (input[0]) {
   output[0] = input[0];
   if (input[1]) {
     output[1] = input[1];
     if (input[2]) {
       output[2] = input[2];
       if (input[3]) {
         output[3] = input[3];
       }
     }
   }
 }
请注意,这会破坏原始的
输入
指针,因此如果需要保留,请复制该指针


请注意,这会破坏原始的
输入
指针,因此,如果需要保留它,请复制一份该指针。

类似这样的内容如何:

char* input = "AB";
char output[4];
// code snippet goes here
// afterward output will be populated with {'A','B',' ',' '}
typedef unsigned int word;
int spacePad(word input) {
    static const word spaces = 0x20202020;

    word mask =
       !input ?                0 :
       !(input & 0x00ffffff) ? 0xff:
       !(input & 0x0000ffff) ? 0xffff :
       !(input & 0x0000ff)   ? 0xffffff :
                               0xffffffff;
    // or without branches
    word branchless_mask =
       1u << (8 * (
         bool(input & 0xff000000) +
         bool(input & 0x00ff0000) +
         bool(input & 0x0000ff00) +
         bool(input & 0x000000ff)
       ));

    return (spaces & mask) | (input & ~mask);
}
typedef无符号整数字;
int空格键(字输入){
静态常量字空间=0x202020;
字掩码=
!输入?0:
!(输入&0x00ffffff)?0xff:
!(输入&0x0000ffff)?0xffff:
!(输入&0x0000ff)?0xffffff:
0xFFFFFF;
//还是没有树枝
无分支字掩码=

1u像这样的事情怎么样:

char* input = "AB";
char output[4];
// code snippet goes here
// afterward output will be populated with {'A','B',' ',' '}
typedef unsigned int word;
int spacePad(word input) {
    static const word spaces = 0x20202020;

    word mask =
       !input ?                0 :
       !(input & 0x00ffffff) ? 0xff:
       !(input & 0x0000ffff) ? 0xffff :
       !(input & 0x0000ff)   ? 0xffffff :
                               0xffffffff;
    // or without branches
    word branchless_mask =
       1u << (8 * (
         bool(input & 0xff000000) +
         bool(input & 0x00ff0000) +
         bool(input & 0x0000ff00) +
         bool(input & 0x000000ff)
       ));

    return (spaces & mask) | (input & ~mask);
}
typedef无符号整数字;
int空格键(字输入){
静态常量字空间=0x202020;
字掩码=
!输入?0:
!(输入&0x00ffffff)?0xff:
!(输入&0x0000ffff)?0xffff:
!(输入&0x0000ff)?0xffffff:
0xFFFFFF;
//还是没有树枝
无分支字掩码=

1u对于这样的短字符串,我不认为您能比简单的实现做得更好:

char buffer[4];

const char * input = "AB";
const char * in = input;
char * out = buffer;
char * end = buffer + sizeof buffer;

while (out < end)
{
    *out = *in != 0 ? *in++ : ' ';
    out++;
}
char缓冲区[4];
常量字符*input=“AB”;
const char*in=输入;
char*out=缓冲区;
char*end=buffer+sizeof buffer;
while(out
对于像这样的短字符串,我不认为您可以做得比简单的实现更好:

char buffer[4];

const char * input = "AB";
const char * in = input;
char * out = buffer;
char * end = buffer + sizeof buffer;

while (out < end)
{
    *out = *in != 0 ? *in++ : ' ';
    out++;
}
char缓冲区[4];
常量字符*input=“AB”;
const char*in=输入;
char*out=缓冲区;
char*end=buffer+sizeof buffer;
while(out
我当然可以使用strcpy或memcpy,但应该可以加快速度,因为在输入长度为4的情况下,他们将查看下一个单词。此外,输出需要填充空格,这可能会产生额外成本。最大长度为4的strncpy可以避免查看下一个单词,但仍然不需要c是空格填充。不,strcpy不会像他要求的那样填充缓冲区。我当然可以使用strcpy或memcpy,但应该可以使其更快,因为在输入长度为4的情况下,他们将查看下一个单词。此外,输出需要填充空格,这可能会产生额外的成本。使用max len的strncpygth为4将避免查看下一个单词,但仍然不考虑空格填充。不,strcpy不会像他要求的那样填充缓冲区。谢谢。我熟悉memcpy,但我正在尝试生成比这更快的内容,因为a)strlen可能会不必要地查看另一个输入单词,b)这不会对输出进行空格填充。o唯一更快的方法是提前知道长度,或者编写自己的
strlen
版本,该版本占用最大长度(有一个非标准的扩展名
strnlen
,正好可以做到这一点).Oh,对于b),如果希望输出以空格结尾,只需将其初始化为空格。
memcpy
只会覆盖它需要的字符并离开空格。谢谢。我熟悉memcpy,但我正在尝试生成比这更快的内容,因为a)strlen可能不必要地查看另一个输入字,b)这是doesn不需要对输出进行空格填充。加快此操作的唯一方法是提前知道长度,或者编写您自己的
strlen
版本,该版本占用最大长度(有一个非标准扩展名
strlen
,正是该扩展名)。哦,对于b),如果您希望输出以空格结尾,只需将其初始化为空格。
memcpy
将只覆盖需要的字符并离开空格。谢谢。我认为这是我能得到的最接近的结果。我一直希望完全没有分支,但这可能不可能。非常感谢。我将继续如果有人提出了没有分支的内容,请在接受之前保留一点。你确定最后一个字节(在
'\0'
之后)将是0吗?@E\u G:添加了一个没有分支的版本我在遵循没有分支的版本时遇到了问题:)对于原始版本,如果说
输入[2]==0