[原][随手写一个] 字符串实现

娄泽豪 17/12/15 22:10:38

明天CET4了,随便写点啥压压惊好了。

// File: heap_string.h
#ifndef __HEADER__HEAP_STRING_H
#define __HEADER__HEAP_STRING_H
typedef struct hstring_t {
    char *data;
    int length;
    int size;
} *hstring_t;

hstring_t hs_create();
void hs_destory(hstring_t str);
void hs_assign(hstring_t str, const char *st);
void hs_resize(hstring_t str, int ref);
const char *hs_get(hstring_t str);
int hs_length(hstring_t str);
const char hs_at(hstring_t str, int idx);
const char hs_at_unsafe(hstring_t str, int idx);
void hs_append_string(hstring_t str, const char *s);
void hs_append_character(hstring_t str, char ch);
void hs_append_integer(hstring_t str, long x);
#define hs_append(str, x) _Generic((x), char *: hs_append_string,    \
                                        int   : hs_append_character, \
                                        char  : hs_append_character, \
                                        long  : hs_append_integer) (str, x)

#endif // __HEADER__HEAP_STRING_H

// File: heap_string.c
#include "heap_string.h"
#include<string.h>
#include<stdlib.h>
hstring_t hs_create()
{
    hstring_t ret = (hstring_t)malloc(sizeof(hstring_t));
    ret->length = 0;
    ret->size = 10;
    ret->data = (char *)malloc(ret->size * sizeof(char));

    return ret;
}

void hs_destory(hstring_t str)
{
    free(str->data);
    free(str);
}

void hs_assign(hstring_t str, const char *st)
{
    int new_length = strlen(st);
    if (new_length < str->length) {
        hs_resize(str, new_length);
    }

    strcpy(str->data, st);
    str->length = new_length;
}

const char *hs_get(hstring_t str)
{
    return str->data;
}

int hs_length(hstring_t str)
{
    return str->length;
}

const char hs_at(hstring_t str, int idx)
{
    if (idx < str->length) {
        return str->data[idx];
    }
    return '\0';
}

const char hs_at_unsafe(hstring_t str, int idx)
{
    return str->data[idx];
}

void hs_resize(hstring_t str, int ref)
{
    static const int MIDVALUE = 1000;
    int new_size = str->size;

    while (new_size < ref) {
        new_size *= (str->size < MIDVALUE) ? 1.5 : 2;
    }

    char *new_buffer = (char *)malloc(new_size * sizeof(char));
    strcpy(new_buffer, str->data);

    free(str->data);
    str->data = new_buffer;
    str->size = new_size;
}

void hs_append_string(hstring_t str, const char *s)
{
    int new_length = str->length + strlen(s);
    if (new_length > str->size) {
        hs_resize(str, new_length);
    }

    strcpy(str->data + str->length, s);
    str->length = new_length;
}

void hs_append_character(hstring_t str, char ch)
{
    int new_length = str->length + sizeof(ch);
    if (new_length > str->size) {
        hs_resize(str, new_length);
    }

    str->data[str->length] = ch;
    str->data[str->length + 1] = '\0';
    str->length++;
}

void hs_append_integer(hstring_t str, long x)
{
    char buffer[25];
    sprintf(buffer, "%ld", x);

    hs_append_string(str, buffer);
}

// File: test.c
#include"heap_string.h"
#include<stdio.h>
int main(int argc, char *argv[])
{
    hstring_t str = hs_create();
    hs_assign(str, "Hello");
    hs_append(str, ',');
    hs_append(str, "world.");
    hs_append(str, 1001010101L);
    puts(hs_get(str));
    hs_destory(str);
}
作者:hepangda 发表于 2017/12/15 22:10:38 原文链接 https://blog.csdn.net/hepangda/article/details/78817222
阅读:70