是否有正在将memmem一个GNU扩展特殊的原因? [英] Is there a particular reason for memmem being a GNU extension?

查看:125
本文介绍了是否有正在将memmem一个GNU扩展特殊的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,将memmem 功能用于定位在一个存储器区域中的字节的特定序列。它可以被类比为的strstr ,这是专门为空结尾的字符串。

In C, the memmem function is used to locate a particular sequence of bytes in a memory area. It can be assimilated to strstr, which is dedicated to null-terminated strings.

有没有什么特别的原因,此函数可作为GNU扩展,而不是直接在标准库?手动状态:

Is there any particular reason for this function to be available as a GNU extension, and not directly in the standard libraries? The manual states :

这个函数在Linux中库破碎并包括libc的5.0.9;有互换针和干草堆参数,并返回一个指向针的第一次出现的结束。

This function was broken in Linux libraries up to and including libc 5.0.9; there the needle and haystack arguments were interchanged, and a pointer to the end of the first occurrence of needle was returned.

新老的libc的有,如果针是空的,草垛,则返回1(而不是大海捞针)的bug。和glibc 2.0使情况变得更糟,返回一个指针草垛的最后一个字节。这是固定的glibc的2.1。

Both old and new libc's have the bug that if needle is empty, haystack-1 (instead of haystack) is returned. And glibc 2.0 makes it worse, returning a pointer to the last byte of haystack. This is fixed in glibc 2.1.

我可以看到它经过多次修复了,但我想知道为什么它不能在某些发行作出直接可用(如果不是更多)为的strstr 。它是否仍然弹出执行问题?

I can see it went through several fixes, yet I'd like to know why it was not made as directly available (if not more) as strstr on some distributions. Does it still bring up implementation issues?

编辑:动机我就不会问这个问题,如果标准决定了它周围的其他方法:包括将memmem 而不是的strstr 。事实上,的strstr 可能是这样的:

Edit : motivations. I wouldn't ask this question if the standard had decided it the other way around : including memmem but not strstr. Indeed, strstr could be something like :

memmem(str, strlen(str), "search", 6); // or
memmem(str, strlen(str) - 1, "search", 6);

稍微棘手,但仍然是一个pretty的逻辑的单行考虑到它是在C函数很平常,要求双方的数据块的和它的长度

Slightly trickier, but still a pretty logical one-liner considering that it is very usual in C functions to require both the data chunk and its length.

修改(2):从意见和答案另一个动机。引用 Theolodis

Edit (2) : another motivation from comments and answers. Quoting Theolodis :

不是每一个功能是必要的每一个,或至少大多数的C语言开发,因此它实际上使标准库不必要的巨大的。

Not every function is necessary to every single, or at least most of the C developers, so it would actually make the standard libraries unnecessarily huge.

好吧,我完全同意,我总是当谈到使得librairies更轻,更快英寸但后来......为什么这两个函数strncpy 的memcpy (从的 keltar 的评论)...?我几乎可以问:?为什么有差将memmem 被黑sheeped

Well, I couldn't agree more, I'm always in when it comes to making the librairies lighter and faster. But then... why both strncpy and memcpy (from keltar's comment) ...? I could almost ask : why has poor memmem been "black-sheeped" ?

推荐答案

从历史上看,是标准的第一次修订之前,C已经被编译器作者的。

Historically, that is before the first revision of the Standard, C has been made by compiler writers.

的strstr 的情况下,这是一个有点不同,因为它已被C委员会介绍,C89理文件告诉我们:

In the case of strstr, it is a little bit different because it has been introduced by the C Committee, the C89 Rationale document tells us that:

的strstr 函数是委员会的发明,它是作为高效的算法钩,或内置子指令。

"The strstr function is an invention of the Committee. It is included as a hook for efficient algorithms, or for built-in substring instruction."

的C委员会没有解释为什么它没有提出一个更一般的功能不仅限于字符串,因此任何理由可能只是猜测。我唯一​​的猜测是用例已经被认为不够重要的是有一个通用的将memmem 而不是的strstr 的。请记住,在C的目标有此要求(在C99理)的保持语言小而简单的的。而且,即使POSIX没有考虑将其加入。

The C Committee does not explain why it has not made a more general function not limited to strings so any reasoning may only be speculation. My only guess is the use case has been considered not important enough to have a generic memmem instead of strstr. Remember that in the goals of C there is this requirement (in the C99 Rationale) "Keep the language small and simple". Also even POSIX didn't consider it for inclusion.

在任何情况下,据我所知,没有人提出任何缺陷报告或提案有将memmem 在内。

In any case to my knowledge nobody has proposed any Defect Report or proposal to have memmem included.

这篇关于是否有正在将memmem一个GNU扩展特殊的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆