/// <summary> /// Appends the first num characters of source to destination, plus a terminating null-character. /// If the length of the C string in source is less than num, only the content up to the terminating null-character is copied. /// </summary> /// <param name="dest">Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character.</param> /// <param name="source">C string to be appended.</param> /// <param name="num">Maximum number of characters to be appended.</param> /// <returns>destination is returned.</returns> public static Pointer <byte> strncat(Pointer <byte> dest, Pointer <byte> source, uint num) { // Seek to the end of dest Pointer <byte> d = dest; while (d.Deref != 0) { d = d.Point(1); } // Start copying uint chars_copied = 0; while (source.Deref != 0 && chars_copied < num) { d[0] = source[0]; chars_copied++; source = source.Point(1); d = d.Point(1); } // Null terminate the destination d[0] = 0; return(dest); }
/// <summary> /// A sequence of calls to this function split str into tokens, which are sequences of contiguous characters /// separated by any of the characters that are part of delimiters. /// /// On a first call, the function expects a C string as argument for str, whose first character is used as the /// starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the /// position right after the end of the last token as the new starting location for scanning. /// /// To determine the beginning and the end of a token, the function first scans from the starting location for /// the first character not contained in delimiters(which becomes the beginning of the token). And then scans /// starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. /// The scan also stops if the terminating null character is found. /// /// This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function. /// Once the terminating null character of str is found in a call to strtok, all subsequent calls to this /// function (with a null pointer as the first argument) return a null pointer. /// /// The point where the last token was found is kept internally by the function to be used on the next call /// (particular library implementations are not required to avoid data races). /// </summary> /// <param name="str">C string to truncate. /// Notice that this string is modified by being broken into smaller strings(tokens). /// Alternativelly, a null pointer may be specified, in which case the function /// continues scanning where a previous successful call to the function ended.</param> /// <param name="delimiters"></param> /// <returns>C string containing the delimiter characters. /// These can be different from one call to another.</returns> public static Pointer <byte> strtok(Pointer <byte> str, Pointer <byte> delimiters) { if (str.IsNull) { str = _last_strtok; if (str.IsNull) { // No more tokens to find return(PointerHelpers.NULL <byte>()); } } // Find the start position int start; int end; for (start = 0; IsInSet(str[start], delimiters) && str[start] != 0; start++) { } // Reached end of string while looking for start if (str[start] == 0) { _last_strtok = PointerHelpers.NULL <byte>(); } // Now look for the end of the token for (end = start + 1; !IsInSet(str[end], delimiters) && str[end] != 0; end++) { } // Reached end of string if (str[end] == 0) { _last_strtok = PointerHelpers.NULL <byte>(); } else { // Insert null token at the end str[end] = 0; // Set state for next search _last_strtok = str.Point(end + 1); } return(str.Point(start)); }
/// <summary> /// Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. /// The matching process does not include the terminating null-characters, but it stops there. /// </summary> /// <param name="str1">The string to look within</param> /// <param name="str2">The string to search for</param> /// <returns>A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.</returns> public static Pointer <byte> strstr(Pointer <byte> str1, Pointer <byte> str2) { uint len1 = strlen(str1); uint len2 = strlen(str2); if (len1 < len2) { return(PointerHelpers.NULL <byte>()); } // Iterate through all potential starting points of the substring for (int c = 0; c <= len1 - len2; c++) { int matchLen = 0; for (matchLen = 0; matchLen < len2; matchLen++) { if (str1[c + matchLen] != str2[matchLen]) { break; } } if (matchLen == len2) { return(str1.Point(c)); } } return(PointerHelpers.NULL <byte>()); }
public static Pointer <byte> strrchr(Pointer <byte> str, byte character) { for (int c = (int)(strlen(str) - 1); c >= 0; c--) { if (str[c] == character) { return(str.Point(c)); } } return(PointerHelpers.NULL <byte>()); }
/// <summary> /// Returns a pointer to the first occurrence of character in the C string str. /// The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string. /// </summary> /// <param name="str">A C string</param> /// <param name="character">Character to be located</param> /// <returns>A pointer to the first occurrence of character in str. ///If the character is not found, the function returns a null pointer.</returns> public static Pointer <byte> strchr(Pointer <byte> str, byte character) { uint c = 0; while (true) { if (str[c] == character) { return(str.Point(c)); } if (str[c] == 0) { return(PointerHelpers.NULL <byte>()); } c++; } }