A pure bytes version of strstr?

断了今生、忘了曾经 提交于 2019-12-01 14:18:42

问题


Is there a version of strstr that works over a fixed length of memory that may include null characters?

I could phrase my question like this: strncpy is to memcpy as strstr is to ?


回答1:


memmem, unfortunately it's GNU-specific rather than standard C. However, it's open-source so you can copy the code (if the license is amenable to you).




回答2:


Not in the standard library (which is not that large, so take a look). However writing your own is trivial, either directly byte by byte or using memchr() followed by memcmp() iteratively.




回答3:


In the standard library, no. However, a quick google search for "safe c string library" turns up several potentially useful results. Without knowing more about the task you are trying to perform, I cannot recommend any particular third-party implementation.

If this is the only "safe" function that you need beyond the standard functions, then it may be best to roll your own rather than expend the effort of integrating a third-party library, provided you are confident that you can do so without introducing additional bugs.



来源:https://stackoverflow.com/questions/1992253/a-pure-bytes-version-of-strstr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!