/**
* @file gensvm_strutil.c
* @author G.J.J. van den Burg
* @date 2014-01-07
* @brief Utility functions for dealing with strings
*
* @details
* This file contains functions for reading files, reading strings from a
* format and checking start and ends of strings.
*
* @copyright
Copyright 2016, G.J.J. van den Burg.
This file is part of GenSVM.
GenSVM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GenSVM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GenSVM. If not, see .
*/
#include "gensvm_strutil.h"
#include "gensvm_print.h"
/**
* @brief Check if a string starts with a prefix
*
* @param[in] str string
* @param[in] pre prefix
* @returns boolean, true if string starts with prefix, false
* otherwise
*/
bool str_startswith(const char *str, const char *pre)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}
/**
* @brief Check if a string ends with a suffix
*
* @param[in] str string
* @param[in] suf suffix
* @returns boolean, true if string ends with suffix, false
* otherwise
*/
bool str_endswith(const char *str, const char *suf)
{
size_t lensuf = strlen(suf),
lenstr = strlen(str);
return lenstr < lensuf ? false : strncmp(str + lenstr - lensuf, suf,
lensuf) == 0;
}
/**
* @brief Check if a string contains a char
*
* @details
* Simple utility function to check if a char occurs in a string.
*
* @param[in] str input string
* @param[in] c character
*
* @return number of times c occurs in str
*/
bool str_contains_char(const char *str, const char c)
{
size_t i, len = strlen(str);
for (i=0; i