What direction should I consider the offset when searching strings in reverse?

I’m making an indexOfReverse utility function in my C++ program, and I have caught a bit of a snag. Implementing the offset and maxOffset in my indexOf was very intuitive to me.. These start from the beginning of the string (+offset), and go forth to the end of the string (or maxOffset if given).

While implementing offsets for the indexOfReverse, though, I was wondering what the most logical way to do this is? Should the offset be from the beginning of the string, or end of the string? Is there an established norm for this I am missing somewhere? I tried explaining it to my duck, but he just looked at me funny. I’m leaning towards thinking of it from the beginning, because of its ease-of-use being more valuable to me than its counter-intuitiveness.. considering the offset from the end just makes things harder to think about in my head.

I have looked at how it’s done in a string library for C++, PHP, and Python, and what seems common in the description of each is finding the last of occurrence of a string where the offset is considered from the beginning, rather than doing true reverse-string searching… But I desire true reverse-string searching rather than last occurrence ofs. Is there a most logical way to consider this, though, so I can move on?

0

Take a look at how it’s done in the C++ standard library: string::rfind. Better yet, just use that function instead of writing your own.

They take offsets from the beginning of the string. Doing so makes it easier to use the result in, say, substr:

string str         = "I haven't done C++ in a while.";
size_t last_in     = str.rfind("in");
string last_phrase = str.substr(last_in); // "in a while."

There’s actually two decisions to make for an rfind function:

  • Should the result index be based on the beginning of the haystack?

    I haven't done C++ in a while.
    0123456789
    

    or the end of it?

    I haven't done C++ in a while.
                         9876543210
    
  • Should the result index be the beginning of the found result?

    I haven't done C++ in a while.
                       ^
    

    or the end of it?

    I haven't done C++ in a while.
                         ^
    

If we answer “end” to either of these questions, we have to answer one more question: does “end” mean the last character?

in
 ^

or the character after it?

in
  ^

The latter is more consistent with the regime of beginning-based indexes.

1

For most string users, measuring the offset from the beginning of the string regardless of the method called would make the most sense.

Recognize that when a user wants to get the last occurrence of something, it does not imply that the user have a logical use of the “reversed string”. If it were so, the user could have simply done a reverse first, and proceed with whatever operation desired. Rather, the user is more likely interested in splitting the string into three or more parts:

Mr. Thomas A. Anderson
<<<|------|>>>>>>>>>>>

Namely, everything before, the matched substring, and everything after.

Because of this, there is a need to pass the string offsets obtained from one method into another method, between find and rfind. Having two definitions just complicates the matter.

A more nuanced approach would be to define an enumerator of matches, a.k.a. Regex. This enumerator allows navigating any number of matches. For each occurrence, the positions of the first character and last character can be queried.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật