bsnes/nall/string/char/trim.hpp

63 lines
1.4 KiB
C++
Raw Normal View History

#ifdef NALL_STRING_INTERNAL_HPP
namespace nall {
Update to v070r14 release. (there was no r13 release posted to the WIP thread) byuu says: - nall/string: trim and split functions now take the limit as a template parameter for clarity, trim_once variants are removed - quotable.trim<1>("\""); //remove quotes from string - cheatcode.split<3>(","); //split up to three times, third one is a description that may have commas - foobar.trim(" "); //remove any and all spaces - nall/string: added wildcard() and iwildcard() functions for pattern matching - nall/directory: accepts an optional pattern parameter to perform wildcard matching - lstring cartridges = directory::contents(path, "*.sfc"); - some people may prefer directory::contents("/path/to/files/*.sfc"), but I like not having to build a string when you have the path separated already - nall/qt: removed entirely, now resides in bsnes/ui-qt/template; I do intend to replace the check/radio actions with native Qt versions later - bsnes/data: new folder, share the parts that both UIs use; bsnes.ico, bsnes.png, bsnes.Desktop, cheats.xml; simplify Makefile install target - Makefile: install target now creates .bsnes folder and copies cheats.xml there for you - Makefile: gconftool hack removed, not needed for phoenix, will work around with Qt later - will probably make bsnes/Qt read the cheats.xml file externally as well, as that file makes each profile 1MB bigger when embedded - as such, will probably make bsnes also look in the binary directory for that file, so Windows users don't have to copy it to their userdata folder
2010-10-11 10:39:14 +00:00
//limit defaults to zero, which will underflow on first compare; equivalent to no limit
template<unsigned Limit> char* ltrim(char* str, const char* key) {
if(!str || !key || !*key) return str;
Update to v070r14 release. (there was no r13 release posted to the WIP thread) byuu says: - nall/string: trim and split functions now take the limit as a template parameter for clarity, trim_once variants are removed - quotable.trim<1>("\""); //remove quotes from string - cheatcode.split<3>(","); //split up to three times, third one is a description that may have commas - foobar.trim(" "); //remove any and all spaces - nall/string: added wildcard() and iwildcard() functions for pattern matching - nall/directory: accepts an optional pattern parameter to perform wildcard matching - lstring cartridges = directory::contents(path, "*.sfc"); - some people may prefer directory::contents("/path/to/files/*.sfc"), but I like not having to build a string when you have the path separated already - nall/qt: removed entirely, now resides in bsnes/ui-qt/template; I do intend to replace the check/radio actions with native Qt versions later - bsnes/data: new folder, share the parts that both UIs use; bsnes.ico, bsnes.png, bsnes.Desktop, cheats.xml; simplify Makefile install target - Makefile: install target now creates .bsnes folder and copies cheats.xml there for you - Makefile: gconftool hack removed, not needed for phoenix, will work around with Qt later - will probably make bsnes/Qt read the cheats.xml file externally as well, as that file makes each profile 1MB bigger when embedded - as such, will probably make bsnes also look in the binary directory for that file, so Windows users don't have to copy it to their userdata folder
2010-10-11 10:39:14 +00:00
unsigned limit = Limit;
while(strbegin(str, key)) {
char* dest = str;
char* src = str + strlen(key);
while(true) {
*dest = *src++;
if(!*dest) break;
dest++;
}
Update to v070r14 release. (there was no r13 release posted to the WIP thread) byuu says: - nall/string: trim and split functions now take the limit as a template parameter for clarity, trim_once variants are removed - quotable.trim<1>("\""); //remove quotes from string - cheatcode.split<3>(","); //split up to three times, third one is a description that may have commas - foobar.trim(" "); //remove any and all spaces - nall/string: added wildcard() and iwildcard() functions for pattern matching - nall/directory: accepts an optional pattern parameter to perform wildcard matching - lstring cartridges = directory::contents(path, "*.sfc"); - some people may prefer directory::contents("/path/to/files/*.sfc"), but I like not having to build a string when you have the path separated already - nall/qt: removed entirely, now resides in bsnes/ui-qt/template; I do intend to replace the check/radio actions with native Qt versions later - bsnes/data: new folder, share the parts that both UIs use; bsnes.ico, bsnes.png, bsnes.Desktop, cheats.xml; simplify Makefile install target - Makefile: install target now creates .bsnes folder and copies cheats.xml there for you - Makefile: gconftool hack removed, not needed for phoenix, will work around with Qt later - will probably make bsnes/Qt read the cheats.xml file externally as well, as that file makes each profile 1MB bigger when embedded - as such, will probably make bsnes also look in the binary directory for that file, so Windows users don't have to copy it to their userdata folder
2010-10-11 10:39:14 +00:00
if(--limit == 0) break;
}
return str;
}
template<unsigned Limit> char* rtrim(char* str, const char* key) {
if(!str || !key || !*key) return str;
Update to v070r14 release. (there was no r13 release posted to the WIP thread) byuu says: - nall/string: trim and split functions now take the limit as a template parameter for clarity, trim_once variants are removed - quotable.trim<1>("\""); //remove quotes from string - cheatcode.split<3>(","); //split up to three times, third one is a description that may have commas - foobar.trim(" "); //remove any and all spaces - nall/string: added wildcard() and iwildcard() functions for pattern matching - nall/directory: accepts an optional pattern parameter to perform wildcard matching - lstring cartridges = directory::contents(path, "*.sfc"); - some people may prefer directory::contents("/path/to/files/*.sfc"), but I like not having to build a string when you have the path separated already - nall/qt: removed entirely, now resides in bsnes/ui-qt/template; I do intend to replace the check/radio actions with native Qt versions later - bsnes/data: new folder, share the parts that both UIs use; bsnes.ico, bsnes.png, bsnes.Desktop, cheats.xml; simplify Makefile install target - Makefile: install target now creates .bsnes folder and copies cheats.xml there for you - Makefile: gconftool hack removed, not needed for phoenix, will work around with Qt later - will probably make bsnes/Qt read the cheats.xml file externally as well, as that file makes each profile 1MB bigger when embedded - as such, will probably make bsnes also look in the binary directory for that file, so Windows users don't have to copy it to their userdata folder
2010-10-11 10:39:14 +00:00
unsigned limit = Limit;
while(strend(str, key)) {
str[strlen(str) - strlen(key)] = 0;
if(--limit == 0) break;
}
return str;
}
template<unsigned limit> char* trim(char* str, const char* key) {
Update to v070r14 release. (there was no r13 release posted to the WIP thread) byuu says: - nall/string: trim and split functions now take the limit as a template parameter for clarity, trim_once variants are removed - quotable.trim<1>("\""); //remove quotes from string - cheatcode.split<3>(","); //split up to three times, third one is a description that may have commas - foobar.trim(" "); //remove any and all spaces - nall/string: added wildcard() and iwildcard() functions for pattern matching - nall/directory: accepts an optional pattern parameter to perform wildcard matching - lstring cartridges = directory::contents(path, "*.sfc"); - some people may prefer directory::contents("/path/to/files/*.sfc"), but I like not having to build a string when you have the path separated already - nall/qt: removed entirely, now resides in bsnes/ui-qt/template; I do intend to replace the check/radio actions with native Qt versions later - bsnes/data: new folder, share the parts that both UIs use; bsnes.ico, bsnes.png, bsnes.Desktop, cheats.xml; simplify Makefile install target - Makefile: install target now creates .bsnes folder and copies cheats.xml there for you - Makefile: gconftool hack removed, not needed for phoenix, will work around with Qt later - will probably make bsnes/Qt read the cheats.xml file externally as well, as that file makes each profile 1MB bigger when embedded - as such, will probably make bsnes also look in the binary directory for that file, so Windows users don't have to copy it to their userdata folder
2010-10-11 10:39:14 +00:00
return ltrim<limit>(rtrim<limit>(str, key), key);
}
template<unsigned limit> char* trim(char* str, const char* lkey, const char* rkey) {
return ltrim<limit>(rtrim<limit>(str, rkey), lkey);
}
//remove whitespace characters from both left and right sides of string
char* strip(char* s) {
if(!s) return nullptr;
signed n = 0, p = 0;
while(s[n]) {
if(s[n] != ' ' && s[n] != '\t' && s[n] != '\r' && s[n] != '\n') break;
n++;
}
while(s[n]) s[p++] = s[n++];
s[p--] = 0;
while(p >= 0) {
if(s[p] != ' ' && s[p] != '\t' && s[p] != '\r' && s[p] != '\n') break;
p--;
}
s[++p] = 0;
return s;
}
}
#endif