A pure bytes version of strstr?

后端 未结 3 957
失恋的感觉
失恋的感觉 2021-01-17 15:08

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

相关标签:
3条回答
  • 2021-01-17 15:16

    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.

    0 讨论(0)
  • 2021-01-17 15:29

    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.

    0 讨论(0)
  • 2021-01-17 15:35

    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).

    0 讨论(0)
提交回复
热议问题