⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yc_string.h

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 H
📖 第 1 页 / 共 2 页
字号:
/*
 * The young Library
 * Copyright (c) 2005 by Yang Huan(杨桓)

 * Permission to use, copy, modify, distribute and sell this software for any
 * purpose is hereby granted without fee, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and this
 * permission notice appear in supporting documentation.
 * The author make no representations about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 */

/******************************************************************************/
/******************************************************************************/
#ifndef __MACRO_C_YOUNG_LIBRARY_DYNAMIC_CHARACTER_STRING_HEADER_FILE__
#define __MACRO_C_YOUNG_LIBRARY_DYNAMIC_CHARACTER_STRING_HEADER_FILE__
/******************************************************************************/
#include <stdio.h>
#include "yc_definition.h"
#include "yc_dymemarr.h"

#ifdef __cplusplus
    namespace youngc {  extern "C" {
#endif
/******************************************************************************/
/******************************************************************************/

enum YOUNG_LIBRARY_STRING_CONSTANT
{
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_C95_WIDE_CHARACTER__
    STATIC_WCSTR_LEN = (sizeof(dymemarray) - sizeof(wchar_t) * 2)
                       / sizeof(wchar_t),
#endif

    STATIC_NCSTR_LEN = (sizeof(dymemarray) - sizeof(char) * 2) / sizeof(char)
};

typedef  struct dynamic_string_object
{
    bool  m_dynamic;
    union
    {
        dymemarray  m_dystr;

#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_C95_WIDE_CHARACTER__
        struct static_wcstring
        {
            wchar_t  len;
            wchar_t  data[STATIC_WCSTR_LEN + 1];
        }  m_wcstr;
#endif

        struct static_ncstring
        {
            unsigned char  len;
            char           data[STATIC_NCSTR_LEN + 1];
        }  m_ncstr;
    }  strobj;
}  dystring, ncstring, wcstring;

/******************************************************************************/

void set_dystr_alloc( ylib_fp_alloc_t alloc );

void set_dystr_dealloc( ylib_fp_dealloc_t dealloc );

size_t hash_ncstring( const ncstring* dycstr );

int cmp_ncstring( const ncstring* left, const ncstring* right );

/* specially function for hash table */
int eq_ncstring( const ncstring* left, const ncstring* right );

#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_C95_WIDE_CHARACTER__
    size_t hash_wcstring( const wcstring* dywcstr );

    int cmp_wcstring( const wcstring* left, const wcstring* right );

    /* specially function for hash table */
    int eq_wcstring( const wcstring* left, const wcstring* right );
#endif

/******************************************************************************/
/******************************************************************************/
/* ncstring */
/******************************************************************************/
/******************************************************************************/

void ncstr_init( ncstring* uninit_self );

void ncstr_destroy( ncstring* self );

/* (1) < 0: uninit_self = src;  (2) = 0: fail;  (3) > 0: succeed */
int ncstr_init_copy( ncstring* uninit_self,
                     const ncstring* src );

/* (1) < 0: self = src or can't matching;  (2) = 0: fail;  (3) > 0: succeed */
int ncstr_assign_copy( ncstring* self,
                       const ncstring* src );

void ncstr_init_move( ncstring* uninit_self,
                      ncstring* src );

void ncstr_assign_move( ncstring* self,
                        ncstring* src );

bool ncstr_push_back( ncstring* self,
                      const char value );

bool ncstr_reserve( ncstring* self,
                    size_t new_capacity );

void ncstr_erase_pos( ncstring* self,
                      size_t index );

void ncstr_erase_range( ncstring* self,
                        size_t first_index,
                        size_t last_index );

bool ncstr_insert_value( ncstring* self,
                         size_t index,
                         const char value,
                         size_t count );

bool ncstr_insert_array( ncstring* self,
                         size_t index,
                         const char* src,
                         size_t count );

bool ncstr_resize( ncstring* self,
                   size_t new_length,
                   const char value );

bool ncstr_replace_fill( ncstring* self,
                         size_t index,
                         size_t old_count,
                         const char value,
                         size_t new_count );

bool ncstr_replace_array( ncstring* self,
                          size_t index,
                          size_t old_count,
                          const char* src,
                          size_t new_count );

bool ncstr_trim( ncstring* self,
                 size_t index,
                 const char* chars,
                 size_t count );

/* format:%[flags] [width] .[precision] [qualifier] [type_char] */
bool ncstr_format( ncstring* self,
                   const char* format,
                   ... );

#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_C95_WIDE_CHARACTER__
    void ncstr_to_wcstr( ncstring* self );
#endif

#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_INPUT_OUTPUT_SYSTEM__
    void ncstr_put( ncstring* self );

    bool ncstr_getline( ncstring* self,
                        const char delimiter );
#endif

#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_FILE_SYSTEM__
    bool ncstr_fgetline( ncstring* self,
                         FILE* fp,
                         const char delimiter );
#endif

#ifndef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_INLINE_FUNCTION__
    size_t ncstr_max_size( void );

    size_t ncstr_size( ncstring* self );

    size_t ncstr_capacity( ncstring* self );

    size_t ncstr_space( ncstring* self );

    char ncstr_front( ncstring* self );

    char ncstr_back( ncstring* self );

    char* ncstr_begin( ncstring* self );

    char* ncstr_end( ncstring* self );

    char* ncstr_index( ncstring* self,
                       size_t index );

    char* ncstr_at( ncstring* self,
                    size_t index );

    const char* ncstr_to_string( ncstring* self );

    void ncstr_pop_back( ncstring* self );
#else
    inline size_t ncstr_max_size( void )
    {
        return SIZE_MAX - 1;
    }

    inline size_t ncstr_size( ncstring* self )
    {
        return false == self->m_dynamic
                        ? self->strobj.m_ncstr.len
                        : DYMEMARR_SIZE( self->strobj.m_dystr, char );
    }

    inline size_t ncstr_capacity( ncstring* self )
    {
        return false == self->m_dynamic
                        ? STATIC_NCSTR_LEN
                        : DYMEMARR_CAPACITY( self->strobj.m_dystr, char );
    }

    inline size_t ncstr_space( ncstring* self )
    {
        return false == self->m_dynamic
                        ? STATIC_NCSTR_LEN - self->strobj.m_ncstr.len
                        : DYMEMARR_SPACE( self->strobj.m_dystr, char );
    }

    inline char ncstr_front( ncstring* self )
    {
        return false == self->m_dynamic
                        ? self->strobj.m_ncstr.data[0]
                        : DYMEMARR_FRONT( self->strobj.m_dystr, char );
    }

    inline char ncstr_back( ncstring* self )
    {
        return false == self->m_dynamic
                        ? self->strobj.m_ncstr.data[self->strobj.m_ncstr.len - 1]
                        : DYMEMARR_BACK( self->strobj.m_dystr, char );
    }

    inline char* ncstr_begin( ncstring* self )
    {
        return false == self->m_dynamic
                        ? self->strobj.m_ncstr.data
                        : DYMEMARR_BEGIN( self->strobj.m_dystr, char );
    }

    inline char* ncstr_end( ncstring* self )
    {
        return false == self->m_dynamic

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -