View Issue Details

IDProjectCategoryView StatusLast Update
0001633Issue 8 draftsBase Definitions and Headerspublic2024-09-05 16:10
Reporternrk Assigned To 
PrioritynormalSeverityCommentTypeEnhancement Request
Status NewResolutionOpen 
NameNickolas Raymond Kaczynski
Organization
User Reference
Section<string.h>
Page Numbern/a
Line Numbern/a
Final Accepted Text
Summary0001633: Add memrchr
Descriptionmemrchr is a simple and useful function that is similar to memchr, except it finds the last occurrence of a byte. It is provided by the following implementations already:

* glibc (https://www.man7.org/linux/man-pages/man3/memrchr.3.html)
* musl
* openbsd (https://man.openbsd.org/memrchr.3)
* freebsd (https://man.freebsd.org/cgi/man.cgi?query=memrchr&manpath=FreeBSD+13.1-RELEASE+and+Ports)

(And probably many others).
Desired ActionUnder section <string.h> add:

void *memrchr(const void *s, int c, size_t n);

Under memchr, add memrchr to SEE ALSO.

Add a new memrchr page:

NAME

    memrchr - find last occurrence of a byte in memory

SYNOPSIS

    #include <string.h>

    void *memrchr(const void *s, int c, size_t n);

DESCRIPTION

    The memrchr() function shall locate the last occurrence of c (converted to an unsigned char) in the initial n bytes (each interpreted as unsigned char) pointed to by s.

RETURN VALUE

    The memrchr() function shall return a pointer to the located byte, or a null pointer if the byte is not found.

ERRORS

    No errors are defined.
TagsNo tags attached.

Activities

Don Cragun

2023-03-02 16:56

manager   bugnote:0006185

In addition to memchr(), the standard also includes wmemchr(). Does this request also intend to add wmemrchr()?

nrk

2023-03-02 17:10

reporter   bugnote:0006186

The purpose of this defect report was to standardize a widely implemented interface.

I've quickly looked into glibc, musl, freebsd & openbsd and none of them seem to provide wmemrchr(). As such, I have no intention of adding wmemrchr().

shware_systems

2023-03-06 16:36

reporter   bugnote:0006194

Note 6186 was discussed in the 2023-03-06 teleconference and it was noted that because wcsrchr() is already present, it functionally serves as an equivalent for most situations to wmemrchr(), and this is a reason there hasn't been much call to implement wmemrchr(). However, for searching blocks that may have embedded wide null chars it is not sufficient; application code is necessary to find the last null before attempting the right scan.

As such, for symmetry and completeness the consensus is more the standard should contain the interface, unless some implementation has a compelling reason it can communicate here why it shouldn't be.

eblake

2024-08-22 15:24

manager   bugnote:0006867

Discussed on the 2024-08-22 call: At this point, we are unaware of any wmemrchr() implementations. While searching backwards for a byte can be done efficiently, searching backwards for the start of a multibyte character can be difficult depending on the encoding, at which point a loop over searching forward until reaching the end of the memory is just as efficient without needing a new specialized function. The consensus for now is that the Austin Group is willing to sponsor memrchr() with a rationale stating why there is no wmemrchr(), which could be amended at a later date if a wmemrchr() is no longer invention.

eblake

2024-09-05 16:10

manager   bugnote:0006875

Regarding 0001633:0006867 - I have been reminded that wmemrchr() is about searching wchar_t not multibyte characters (searching backwards on multibyte characters is not trivial, but searching wchar_t can be efficient); but the point remains that the Austin Group is unaware of any implementations of wmemrchr() or applications that would benefit from having it.

Issue History

Date Modified Username Field Change
2023-02-16 10:07 nrk New Issue
2023-02-16 10:07 nrk Name => Nickolas Raymond Kaczynski
2023-02-16 10:07 nrk Section => <string.h>
2023-02-16 10:07 nrk Page Number => n/a
2023-02-16 10:07 nrk Line Number => n/a
2023-03-02 16:56 Don Cragun Note Added: 0006185
2023-03-02 17:10 nrk Note Added: 0006186
2023-03-06 16:36 shware_systems Note Added: 0006194
2024-08-22 15:24 eblake Note Added: 0006867
2024-09-05 16:10 eblake Note Added: 0006875