Path: blob/master/thirdparty/sdl/stdlib/SDL_strtokr.c
9903 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"212223char *SDL_strtok_r(char *s1, const char *s2, char **ptr)24{25#ifdef HAVE_STRTOK_R26return strtok_r(s1, s2, ptr);2728#else /* SDL implementation */29/*30* Adapted from _PDCLIB_strtok() of PDClib library at31* https://github.com/DevSolar/pdclib.git32*33* The code was under CC0 license:34* https://creativecommons.org/publicdomain/zero/1.0/legalcode :35*36* No Copyright37*38* The person who associated a work with this deed has dedicated the39* work to the public domain by waiving all of his or her rights to40* the work worldwide under copyright law, including all related and41* neighboring rights, to the extent allowed by law.42*43* You can copy, modify, distribute and perform the work, even for44* commercial purposes, all without asking permission. See Other45* Information below.46*/47const char *p = s2;4849if (!s2 || !ptr || (!s1 && !*ptr)) return NULL;5051if (s1 != NULL) { /* new string */52*ptr = s1;53} else { /* old string continued */54if (*ptr == NULL) {55/* No old string, no new string, nothing to do */56return NULL;57}58s1 = *ptr;59}6061/* skip leading s2 characters */62while (*p && *s1) {63if (*s1 == *p) {64/* found separator; skip and start over */65++s1;66p = s2;67continue;68}69++p;70}7172if (! *s1) { /* no more to parse */73*ptr = s1;74return NULL;75}7677/* skipping non-s2 characters */78*ptr = s1;79while (**ptr) {80p = s2;81while (*p) {82if (**ptr == *p++) {83/* found separator; overwrite with '\0', position *ptr, return */84*((*ptr)++) = '\0';85return s1;86}87}88++(*ptr);89}9091/* parsed to end of string */92return s1;93#endif94}959697