@@ -168,9 +168,7 @@ class ps_ptr {
168168 }
169169
170170 // 🆕 alloc constructor, e.g. ps_ptr<char>buff(1024)
171- explicit ps_ptr (size_t n) {
172- alloc (n);
173- }
171+ explicit ps_ptr (size_t n) { alloc (n); }
174172 // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
175173 // 📌📌📌 A L L O C 📌📌📌
176174
@@ -1196,12 +1194,16 @@ class ps_ptr {
11961194
11971195 template <typename U = T>
11981196 requires std::is_same_v<U, char >
1199- int last_index_of (char ch) const {
1197+ int last_index_of (char ch, int start_pos = - 1 ) const {
12001198 if (!mem) return -1 ;
12011199
12021200 const char * str = static_cast <const char *>(mem.get ());
12031201 int len = static_cast <int >(std::strlen (str));
1204- for (int i = len - 1 ; i >= 0 ; --i) {
1202+
1203+ // if no start position is specified, start at the end
1204+ if (start_pos < 0 || start_pos >= len) start_pos = len - 1 ;
1205+
1206+ for (int i = start_pos; i >= 0 ; --i) {
12051207 if (str[i] == ch) return i;
12061208 }
12071209 return -1 ;
@@ -1214,12 +1216,20 @@ class ps_ptr {
12141216 // printf("last_i %i\n", last_i);
12151217 // printf("last_A %i\n", last_A);
12161218
1217- int last_index_of (const T& value) const {
1219+ // ps_ptr<char> str;
1220+ // str.assign("/audiofiles/my_playlist/podcast/h.mp3");
1221+ // int last = str.last_index_of('/'); // → 32 (the last '/')
1222+ // int prev = str.last_index_of('/', last - 1); // → 23 (the second to last '/')
1223+
1224+ int last_index_of (const T& value, int start_pos = -1 ) const {
12181225 if (!mem || allocated_size < sizeof (T)) return -1 ;
12191226
12201227 std::size_t count = allocated_size / sizeof (T);
12211228 T* data = get ();
1222- for (int i = static_cast <int >(count) - 1 ; i >= 0 ; --i) {
1229+
1230+ if (start_pos < 0 || start_pos >= static_cast <int >(count)) start_pos = static_cast <int >(count) - 1 ;
1231+
1232+ for (int i = start_pos; i >= 0 ; --i) {
12231233 if (data[i] == value) return i;
12241234 }
12251235 return -1 ;
0 commit comments