/*
 * SPDX-FileCopyrightText: (c) 2009 RWTH Aachen University <rwth-aachen.de>
 * SPDX-FileCopyrightText: (c) 2009 Gesellschaft für numerische Simulation mbH Braunschweig <gns-mbh.com>
 * SPDX-FileCopyrightText: (c) 2009 Technische Universität Dresden <tu-dresden.de>
 * SPDX-FileCopyrightText: (c) 2009 University of Oregon <uoregon.edu>
 * SPDX-FileCopyrightText: (c) 2009 Forschungszentrum Jülich GmbH <fz-juelich.de>
 * SPDX-FileCopyrightText: (c) 2009 German Research School for Simulation Sciences GmbH
 * SPDX-FileCopyrightText: (c) 2009 Technische Universität München <tum.de>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

/**
 * @file
 * @ingroup         UTILS_CStr_module
 *
 * @brief           Helper functions for string handling
 */

#include <config.h>
#include <UTILS_CStr.h>

#include <string.h>
#include <stdlib.h>

#include <UTILS_Error.h>

const size_t UTILS_CStr_npos = ( size_t )-1;


char*
UTILS_CStr_dup( const char* source )
{
    if ( source == NULL )
    {
        return NULL;
    }
    char* dup = ( char* )malloc( strlen( source ) + 1 );
    if ( dup == NULL )
    {
        UTILS_ERROR_POSIX();
        return NULL;
    }
    strcpy( dup, source );
    return dup;
}

size_t
UTILS_CStr_find( const char* str,
                 const char* pattern,
                 size_t      pos )
{
    const size_t length = strlen( pattern );

    for (; str[ pos ] != '\0'; pos++ )
    {
        if ( strncmp( &str[ pos ], pattern, length ) == 0 )
        {
            return pos;
        }
    }
    return UTILS_CStr_npos;
}
