src/cx/string.h

changeset 1668
3ffdfe1776b4
parent 1667
608cc0b25352
child 1671
cf19b7820ff0
--- a/src/cx/string.h	Thu Dec 25 11:10:13 2025 +0100
+++ b/src/cx/string.h	Thu Dec 25 11:39:26 2025 +0100
@@ -531,6 +531,44 @@
 CX_EXPORT cxmutstr cx_strsubsl_m(cxmutstr string, size_t start, size_t length);
 
 /**
+ * Returns the character at the specified index offset.
+ *
+ * Internal function - do not use.
+ *
+ * @param str the string
+ * @param index the index offset
+ * @return the character at the index
+ * @see cx_strat()
+ */
+CX_INLINE char cx_strat_(cxstring str, off_t index) {
+    size_t i;
+    if (index >= 0) {
+        i = index;
+    } else {
+        i = (size_t) (str.length + index);
+    }
+    if (i >= str.length) {
+        return '\0';
+    }
+    return str.ptr[i];
+}
+
+/**
+ * Returns the character at the specified index offset.
+ *
+ * When the @p index is negative, the character is counted from the end of the
+ * string where -1 denotes the last character in the string.
+ *
+ * When the @p index is out of bounds, the function returns zero.
+ *
+ * @param str the string
+ * @param index the index offset
+ * @return the character at the index
+ * @see cx_strat()
+ */
+#define cx_strat(str, index) cx_strat_(cx_strcast(str), index)
+
+/**
  * Returns a substring starting at the location of the first occurrence of the
  * specified character.
  *

mercurial