c_util
C utility library for low-level programming
Loading...
Searching...
No Matches
core_string.h File Reference

core_string_tオブジェクトの定義と関連APIの宣言 More...

#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Classes

struct  core_string_t
 文字列オブジェクト構造体 More...
 

Macros

#define CORE_STRING_INITIALIZER   { 0 }
 オブジェクト初期化用マクロ
 

Typedefs

typedef enum CORE_STRING_ERROR_CODE CORE_STRING_ERROR_CODE
 core_string_t関連処理が出力するエラーコード
 
typedef struct core_string_t core_string_t
 文字列オブジェクト構造体
 

Enumerations

enum  CORE_STRING_ERROR_CODE {
  CORE_STRING_SUCCESS , CORE_STRING_INVALID_ARGUMENT , CORE_STRING_RUNTIME_ERROR , CORE_STRING_BUFFER_EMPTY ,
  CORE_STRING_MEMORY_ALLOCATE_ERROR
}
 core_string_t関連処理が出力するエラーコード More...
 

Functions

void core_string_default_create (core_string_t *const string_)
 引数で与えたstring_オブジェクトを「デフォルト状態」に初期化する。
 
CORE_STRING_ERROR_CODE core_string_create (const char *const src_, core_string_t *const dst_)
 引数のsrc_をコピーして、dst_オブジェクトを初期化済み状態にする。
 
CORE_STRING_ERROR_CODE core_string_copy (const core_string_t *const src_, core_string_t *const dst_)
 コピー元src_の文字列内容を、コピー先dst_に複製する。
 
CORE_STRING_ERROR_CODE core_string_copy_from_char (const char *const src_, core_string_t *const dst_)
 dst_にchar*型文字列をコピーする
 
void core_string_destroy (core_string_t *const string_)
 string_が保持するメモリを破棄する。
 
CORE_STRING_ERROR_CODE core_string_buffer_reserve (uint64_t buffer_size_, core_string_t *const string_)
 string_オブジェクトに指定サイズの文字列バッファを確保する。
 
CORE_STRING_ERROR_CODE core_string_buffer_resize (uint64_t buffer_size_, core_string_t *const string_)
 core_string_tオブジェクトのバッファサイズをbuffer_size_に拡張する
 
uint64_t core_string_buffer_capacity (const core_string_t *const string_)
 core_string_tオブジェクトのバッファサイズを取得する。
 
bool core_string_is_empty (const core_string_t *const string_)
 core_string_tオブジェクトが保持する文字列格納バッファが空かどうかを判定する
 
bool core_string_equal (const core_string_t *const string1_, const core_string_t *const string2_)
 2つのcore_string_tオブジェクトが保持する文字列が等しいかを比較する
 
bool core_string_equal_from_char (const char *const str1_, const core_string_t *const string2_)
 char*型文字列とcore_string_tオブジェクトが保持する文字列が等しいかを比較する
 
uint64_t core_string_length (const core_string_t *const string_)
 core_string_tオブジェクトが保持している文字列の長さを取得する
 
const char * core_string_cstr (const core_string_t *const string_)
 core_string_tオブジェクトが保持している文字列の先頭ポインタを取得する
 
CORE_STRING_ERROR_CODE core_string_concat (const core_string_t *const string_, core_string_t *const dst_)
 dst_の末尾にstring_の内容を連結する
 
CORE_STRING_ERROR_CODE core_string_substring_copy (const core_string_t *const src_, core_string_t *const dst_, uint16_t from_, uint16_t to_)
 src_ の [from_, to_] 範囲の部分文字列を dst_ にコピーする
 
CORE_STRING_ERROR_CODE core_string_trim (const core_string_t *const src_, core_string_t *const dst_, char ltrim_, char rtrim_)
 指定したトリム文字を取り除いた文字列をdst_にコピーする。
 
CORE_STRING_ERROR_CODE core_string_to_i32 (const core_string_t *const string_, int32_t *out_value_)
 core_string_tオブジェクトの内容をint32_t型整数に変換する。
 

Detailed Description

core_string_tオブジェクトの定義と関連APIの宣言

Author
chocolate-pie24

core_string_tは、ユーザーが文字列長さやバッファ管理を意識することなく、 安全かつ簡単に文字列操作を行えることを目的としたAPIである。

代表的な操作として以下が提供される:

  • 文字列のコピー
  • 文字列の連結
  • 文字列のトリミング

本APIでは、core_string_t型の扱いにおいて以下の状態を区別する:

  • NULLポインタ: オブジェクト自体がNULLの場合(例: core_string_t* object = 0)
  • 未初期化状態: オブジェクト自体は存在するが、メンバの初期化がされていない状態(例: core_string_t object;)
  • デフォルト状態: オブジェクト内部管理データinternal_data == NULLの状態。使用前に明示的な初期化が必要。
  • 初期化済み状態: internal_dataが有効な領域を指しており、APIでの使用が可能な状態。

未初期化状態のオブジェクトをAPIに渡すと未定義の動作を引き起こす可能性があるため、 必ず次のいずれかの方法で「デフォルト状態」に初期化してから使用すること:

その後、以下のいずれかの関数で「初期化済み状態」に遷移させる必要がある:

再初期化する場合は、先に core_string_destroy() を呼び、メモリを解放すること。

Version
0.1
Date
2025-07-20

Macro Definition Documentation

◆ CORE_STRING_INITIALIZER

#define CORE_STRING_INITIALIZER   { 0 }

オブジェクト初期化用マクロ

使用例:

#define CORE_STRING_INITIALIZER
オブジェクト初期化用マクロ
Definition core_string.h:78
文字列オブジェクト構造体
Definition core_string.h:67

Typedef Documentation

◆ CORE_STRING_ERROR_CODE

core_string_t関連処理が出力するエラーコード

◆ core_string_t

typedef struct core_string_t core_string_t

文字列オブジェクト構造体

文字列データとそれに付随する管理情報を格納する。 オブジェクトの初期化については、 core_string_initialization_rule を参照のこと。

Enumeration Type Documentation

◆ CORE_STRING_ERROR_CODE

core_string_t関連処理が出力するエラーコード

Enumerator
CORE_STRING_SUCCESS 

正常終了

CORE_STRING_INVALID_ARGUMENT 

引数異常

CORE_STRING_RUNTIME_ERROR 

実行時エラー

CORE_STRING_BUFFER_EMPTY 

文字列バッファが空

CORE_STRING_MEMORY_ALLOCATE_ERROR 

メモリアロケートエラー

Function Documentation

◆ core_string_buffer_capacity()

uint64_t core_string_buffer_capacity ( const core_string_t *const string_)

core_string_tオブジェクトのバッファサイズを取得する。

Note

使用例:

uint64_t capacity = core_string_buffer_capacity(&string); // 未初期化のため0が返る
uint64_t core_string_buffer_capacity(const core_string_t *const string_)
core_string_tオブジェクトのバッファサイズを取得する。
Definition core_string.c:248
Parameters
[in]string_バッファサイズを取得する対象オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
INVALID_VALUE_U64引数string_がNULL
0オブジェクトがデフォルト状態、またはバッファサイズが0
バッファサイズ(byte)正常に取得できた場合のバッファサイズを返す
See also
core_string_buffer_reserve()
core_string_buffer_resize()

◆ core_string_buffer_reserve()

CORE_STRING_ERROR_CODE core_string_buffer_reserve ( uint64_t buffer_size_,
core_string_t *const string_ )

string_オブジェクトに指定サイズの文字列バッファを確保する。

Note
  • string_がデフォルト状態( core_string_initialization_rule 参照)の場合、internal_data構造体を確保して初期化する
  • すでに指定したサイズ以上のバッファが確保されている場合には何もせず成功を返す
  • バッファの再確保が行われた場合、既存のデータはすべて破棄され、バッファ全体がクリアされる

使用例:

CORE_STRING_ERROR_CODE result = core_string_buffer_reserve(128, &string); // 128byte確保
if(CORE_STRING_SUCCESS != result) {
// エラー処理
}
CORE_STRING_ERROR_CODE core_string_buffer_reserve(uint64_t buffer_size_, core_string_t *const string_)
string_オブジェクトに指定サイズの文字列バッファを確保する。
Definition core_string.c:168
CORE_STRING_ERROR_CODE
core_string_t関連処理が出力するエラーコード
Definition core_string.h:53
@ CORE_STRING_SUCCESS
Definition core_string.h:54
void core_string_destroy(core_string_t *const string_)
string_が保持するメモリを破棄する。
Definition core_string.c:155
Parameters
[in]buffer_size_確保するバッファサイズ(byte)(終端文字を含むサイズを指定する)(文字列長 + 1)
[out]string_メモリ確保対象オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
CORE_STRING_INVALID_ARGUMENT引数string_がNULL
CORE_STRING_MEMORY_ALLOCATE_ERRORメモリ確保に失敗
CORE_STRING_SUCCESSメモリ確保が正常に完了
See also
core_string_buffer_capacity()
core_malloc()
core_free()
core_string_destroy()
core_zero_memory()

◆ core_string_buffer_resize()

CORE_STRING_ERROR_CODE core_string_buffer_resize ( uint64_t buffer_size_,
core_string_t *const string_ )

core_string_tオブジェクトのバッファサイズをbuffer_size_に拡張する

Note
  • この関数は既存の文字列データを保持したままバッファサイズを拡張する
  • すでに指定サイズ以上のバッファが確保されている場合は何もせず成功を返す
  • オブジェクトがデフォルト状態の場合は core_string_buffer_reserve() を呼び出して新規に確保する( core_string_initialization_rule 参照)

使用例:

core_string_copy_from_char("Hello", &string); // 現在 "Hello" を保持
core_string_buffer_resize(128, &string); // バッファを128byteに拡張
CORE_STRING_ERROR_CODE core_string_copy_from_char(const char *const src_, core_string_t *const dst_)
dst_にchar*型文字列をコピーする
Definition core_string.c:129
CORE_STRING_ERROR_CODE core_string_buffer_resize(uint64_t buffer_size_, core_string_t *const string_)
core_string_tオブジェクトのバッファサイズをbuffer_size_に拡張する
Definition core_string.c:198
Parameters
[in]buffer_size_新しいバッファサイズ(byte) 終端文字を含めるため「文字列長 + 1」を指定すること
[in,out]string_バッファサイズを拡張する対象オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
CORE_STRING_INVALID_ARGUMENT引数string_がNULL
CORE_STRING_MEMORY_ALLOCATE_ERROR一時バッファまたは新規バッファのメモリ確保に失敗
CORE_STRING_RUNTIME_ERROR一時バッファへのコピーまたは戻しに失敗
CORE_STRING_SUCCESS正常にリサイズが完了、または既に十分な容量が確保されている
See also
core_string_buffer_reserve()
core_string_buffer_capacity()
core_malloc()
core_free()

◆ core_string_concat()

CORE_STRING_ERROR_CODE core_string_concat ( const core_string_t *const string_,
core_string_t *const dst_ )

dst_の末尾にstring_の内容を連結する

Note
  • 連結の結果、dst_のバッファサイズが不足する場合は自動でリサイズが行われる
  • string_およびdst_がデフォルト状態( core_string_initialization_rule 参照)の場合はエラーを返す
  • 連結後、dst_の内部のバッファ末尾には必ず終端文字 '\0' が付加される

使用例:

core_string_create("Hello", &dst);
core_string_create(" World", &add);
if (CORE_STRING_SUCCESS != result) {
// エラー処理
}
printf("%s\n", core_string_cstr(&dst)); // Hello World
CORE_STRING_ERROR_CODE core_string_create(const char *const src_, core_string_t *const dst_)
引数のsrc_をコピーして、dst_オブジェクトを初期化済み状態にする。
Definition core_string.c:68
CORE_STRING_ERROR_CODE core_string_concat(const core_string_t *const string_, core_string_t *const dst_)
dst_の末尾にstring_の内容を連結する
Definition core_string.c:349
const char * core_string_cstr(const core_string_t *const string_)
core_string_tオブジェクトが保持している文字列の先頭ポインタを取得する
Definition core_string.c:336
Parameters
[in]string_連結する文字列 (コピー元)。未初期化状態は不可。( core_string_initialization_rule 参照)
[out]dst_連結結果を格納するオブジェクト (コピー先)。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
CORE_STRING_INVALID_ARGUMENTstring_またはdst_が NULL
CORE_STRING_RUNTIME_ERRORstring_ またはdst_がデフォルト状態( core_string_initialization_rule 参照)、または一時バッファの生成やコピーに失敗した場合
CORE_STRING_SUCCESS連結が正常に完了
See also
core_string_buffer_capacity()
core_string_buffer_resize()
core_string_cstr()
core_string_destroy()

◆ core_string_copy()

CORE_STRING_ERROR_CODE core_string_copy ( const core_string_t *const src_,
core_string_t *const dst_ )

コピー元src_の文字列内容を、コピー先dst_に複製する。

Note
  • dst_がデフォルト状態( core_string_initialization_rule 参照)またはバッファサイズ不足の場合、必要なサイズでバッファを再確保する。
  • dst_のバッファサイズが src_以上の場合、既存バッファを再利用して内容を上書きする。

使用例:

core_string_create("Hello", &src); // コピー元を作成
CORE_STRING_ERROR_CODE result = core_string_copy(&src, &dst); // src→dstへコピー
if (CORE_STRING_SUCCESS != result) {
// エラー処理
}
CORE_STRING_ERROR_CODE core_string_copy(const core_string_t *const src_, core_string_t *const dst_)
コピー元src_の文字列内容を、コピー先dst_に複製する。
Definition core_string.c:92
Parameters
[in]src_コピー元オブジェクト(初期化済み状態である必要あり)( core_string_initialization_rule 参照)
[out]dst_コピー先オブジェクト
Return values
CORE_STRING_INVALID_ARGUMENT引数src_またはdst_がNULL
CORE_STRING_RUNTIME_ERRORコピー元src_がデフォルト状態( core_string_initialization_rule 参照)、または文字列コピーに失敗
CORE_STRING_BUFFER_EMPTYコピー元オブジェクトsrc_が内部で保持している文字列が空
CORE_STRING_MEMORY_ALLOCATE_ERRORコピー先オブジェクトのメモリ確保に失敗
CORE_STRING_SUCCESS正常にコピーが完了
See also
core_string_create()
core_string_default_create()
core_string_buffer_capacity()
core_string_buffer_reserve()
core_zero_memory()
core_string_destroy()

◆ core_string_copy_from_char()

CORE_STRING_ERROR_CODE core_string_copy_from_char ( const char *const src_,
core_string_t *const dst_ )

dst_にchar*型文字列をコピーする

Note
  • dst_デフォルト状態( core_string_initialization_rule 参照)またはバッファサイズ不足の場合、必要なサイズでバッファを再確保する。
  • dst_のバッファサイズが src_以上の場合、既存バッファを再利用して内容を上書きする。

使用例:

if(CORE_STRING_SUCCESS != result) {
// エラー処理
}
Parameters
[in]src_コピー元文字列
[out]dst_コピー先文字列オブジェクト(デフォルト状態または初期化済み状態である必要あり)( core_string_initialization_rule 参照)
Return values
CORE_STRING_INVALID_ARGUMENT引数src_またはdst_がNULL
CORE_STRING_MEMORY_ALLOCATE_ERRORコピー先オブジェクトのメモリ確保に失敗
CORE_STRING_RUNTIME_ERROR文字列のコピーに失敗
CORE_STRING_SUCCESS正常にコピーが完了
See also
core_string_buffer_reserve()
core_zero_memory()
core_string_destroy()

◆ core_string_create()

CORE_STRING_ERROR_CODE core_string_create ( const char *const src_,
core_string_t *const dst_ )

引数のsrc_をコピーして、dst_オブジェクトを初期化済み状態にする。

Note
本関数は、未初期化状態・デフォルト状態いずれのオブジェクトにも使用可能である。 各状態の詳細については、core_string_initialization_rule を参照のこと。
この関数の内部では core_string_destroy() が呼び出されるため、 dst_がすでに初期化済みで内部にデータを保持している場合は、 保持しているメモリがすべて解放された後に再初期化される。

使用例:

core_string_t dst = CORE_STRING_INITIALIZER; // オブジェクトをデフォルト状態にする
CORE_STRING_ERROR_CODE result = core_string_create("Hello", &dst); // dstにHelloが格納され、オブジェクトが初期化済み状態となる
if(CORE_STRING_SUCCESS != result) {
// ここにエラー処理を書く
}
Parameters
[in]src_初期化文字列(終端文字まで含めてコピーされる)
[out]dst_初期化対象オブジェクト
Return values
CORE_STRING_INVALID_ARGUMENT引数src_またはdst_がNULL
CORE_STRING_RUNTIME_ERROR文字列のコピーに失敗(これが発生したら本関数のバグ)
CORE_STRING_MEMORY_ALLOCATE_ERROR文字列バッファメモリの確保に失敗
CORE_STRING_SUCCESS正常に初期化とコピーが完了
See also
core_string_destroy()
core_string_default_create()
core_string_buffer_reserve()

◆ core_string_cstr()

const char * core_string_cstr ( const core_string_t *const string_)

core_string_tオブジェクトが保持している文字列の先頭ポインタを取得する

Note
  • 戻り値は内部バッファへのポインタであり、呼び出し側が解放してはいけない
  • オブジェクトがデフォルト状態( core_string_initialization_rule 参照)、または引数が NULL の場合は NULL を返す
  • 戻り値は不変ではなく、core_string_tの内容が変更されると無効になる可能性がある

使用例:

core_string_create("Hello", &str);
const char* cstr = core_string_cstr(&str); // cstr = "Hello"
printf("%s\n", cstr);
Parameters
[in]string_c文字列を取得する対象のcore_string_tオブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
NULL引数がNULLまたはデフォルト状態( core_string_initialization_rule 参照)のオブジェクトが渡された場合
その他内部バッファの先頭アドレス
See also
core_string_create()
core_string_destroy()
core_string_length()

◆ core_string_default_create()

void core_string_default_create ( core_string_t *const string_)

引数で与えたstring_オブジェクトを「デフォルト状態」に初期化する。

Note
オブジェクトのデフォルト状態については、 core_string_initialization_rule を参照のこと。
内部にデータを保持している初期化済みオブジェクトに対して本関数を直接呼ぶと、 内部データのメモリが解放されず、メモリリークの原因となる可能性がある。 再利用する場合は、必ず事前に core_string_destroy() を呼んでメモリを解放してから使用すること。
引数string_にNULLを与えた場合には、以下のワーニングメッセージを出力し、処理を終了する。 [WARNING] core_string_default_create - Argument string_ requires a valid pointer.

使用例:

core_string_default_create(&string); // オブジェクトがデフォルト状態となる(internal_dataはNULLとなる)。
void core_string_default_create(core_string_t *const string_)
引数で与えたstring_オブジェクトを「デフォルト状態」に初期化する。
Definition core_string.c:63

なお、上記コードは下記のコードと等価である。

Parameters
[in,out]string_デフォルト状態とするオブジェクト
See also
core_string_destroy()
core_zero_memory()

◆ core_string_destroy()

void core_string_destroy ( core_string_t *const string_)

string_が保持するメモリを破棄する。

Note
この関数を呼ぶことで、core_string_t型オブジェクトが保持しているinternal_dataのメモリおよび、 internal_data内に保持しているメモリ領域が解放され、NULLが設定される。 これにより、internal_dataにはNULLが設定される。なお、メモリの解放にはcore_free()を使用している。
本関数により破棄したオブジェクトを再度使用する場合には、下記の関数を使用する。
引数string_にNULLを与えた場合には、以下のワーニングメッセージを出力し、処理を終了する。 [WARNING] core_string_destroy - Argument string_ requires a valid pointer.

使用例:

core_string_destroy(&string); // stringが保持するメモリが解放される
Parameters
[in,out]string_破棄対象オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
See also
core_free()

◆ core_string_equal()

bool core_string_equal ( const core_string_t *const string1_,
const core_string_t *const string2_ )

2つのcore_string_tオブジェクトが保持する文字列が等しいかを比較する

Note
  • どちらかの引数がデフォルト状態またはNULLの場合はfalseを返す( core_string_initialization_rule 参照)
  • 比較はバッファの長さと各文字列の中身が完全一致するかどうかで判定される

使用例:

core_string_create("Hello", &str1);
core_string_create("Hello", &str2);
if(core_string_equal(&str1, &str2)) {
// 文字列が一致
}
bool core_string_equal(const core_string_t *const string1_, const core_string_t *const string2_)
2つのcore_string_tオブジェクトが保持する文字列が等しいかを比較する
Definition core_string.c:275
Parameters
[in]string1_比較対象1。未初期化状態は不可。( core_string_initialization_rule 参照)
[in]string2_比較対象2。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
true2つの文字列が等しい
false引数がNULL/デフォルト状態( core_string_initialization_rule 参照) / 文字列長や中身が一致しない場合
See also
core_string_create()
core_string_destroy()

◆ core_string_equal_from_char()

bool core_string_equal_from_char ( const char *const str1_,
const core_string_t *const string2_ )

char*型文字列とcore_string_tオブジェクトが保持する文字列が等しいかを比較する

Note
  • 引数のどちらかが NULL またはデフォルト状態( core_string_initialization_rule 参照)の場合は false を返す
  • 比較は文字列長と各文字が完全一致するかどうかで判定される

使用例:

core_string_create("Hello", &str2);
if(core_string_equal_from_char("Hello", &str2)) {
// 一致
}
bool core_string_equal_from_char(const char *const str1_, const core_string_t *const string2_)
char*型文字列とcore_string_tオブジェクトが保持する文字列が等しいかを比較する
Definition core_string.c:301
Parameters
[in]str1_比較対象の文字列
[in]string2_比較対象のcore_string_tオブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
true文字列が等しい
false引数が NULL / デフォルト状態( core_string_initialization_rule 参照) / 文字列長や中身が一致しない場合
See also
core_string_equal()
core_string_create()
core_string_destroy()

◆ core_string_is_empty()

bool core_string_is_empty ( const core_string_t *const string_)

core_string_tオブジェクトが保持する文字列格納バッファが空かどうかを判定する

Note
  • 引数がNULLまたはデフォルト状態( core_string_initialization_rule 参照)の場合は true を返す
  • 長さが0の文字列を保持している場合も true を返す

使用例:

if (core_string_is_empty(&string)) {
// まだ初期化されていない or 空文字列
}
bool core_string_is_empty(const core_string_t *const string_)
core_string_tオブジェクトが保持する文字列格納バッファが空かどうかを判定する
Definition core_string.c:260
Parameters
[in]string_判定対象オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
trueオブジェクトがNULL / デフォルト状態( core_string_initialization_rule 参照) / 空文字列である
false文字列が1文字以上存在する
See also
core_string_length()
core_string_buffer_capacity()

◆ core_string_length()

uint64_t core_string_length ( const core_string_t *const string_)

core_string_tオブジェクトが保持している文字列の長さを取得する

Note

使用例:

core_string_create("Hello", &str);
uint64_t len = core_string_length(&str); // len = 5
uint64_t core_string_length(const core_string_t *const string_)
core_string_tオブジェクトが保持している文字列の長さを取得する
Definition core_string.c:323
Parameters
[in]string_長さを取得する対象のcore_string_tオブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
Return values
INVALID_VALUE_U64引数が NULL の場合
0未初期化のオブジェクトが渡された場合
その他保持している文字列の長さ
See also
core_string_create()
core_string_destroy()

◆ core_string_substring_copy()

CORE_STRING_ERROR_CODE core_string_substring_copy ( const core_string_t *const src_,
core_string_t *const dst_,
uint16_t from_,
uint16_t to_ )

src_ の [from_, to_] 範囲の部分文字列を dst_ にコピーする

Note
  • from_からto_ の範囲は両端を含むインデックスとして扱われる
  • dst_のバッファサイズが不足している場合は自動でリサイズが行われる
  • 部分文字列のコピー後、dst_の末尾には必ず終端文字'\0'が付加される

使用例:

core_string_create("Hello World", &src);
if (CORE_STRING_SUCCESS != result) {
// エラー処理
}
printf("%s\n", core_string_cstr(&dst)); // "World"
CORE_STRING_ERROR_CODE core_string_substring_copy(const core_string_t *const src_, core_string_t *const dst_, uint16_t from_, uint16_t to_)
src_ の [from_, to_] 範囲の部分文字列を dst_ にコピーする
Definition core_string.c:389
Parameters
[in]src_部分文字列を抽出する元の文字列オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
[out]dst_抽出結果を格納する文字列オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
[in]from_抽出を開始するインデックス (0-based)
[in]to_抽出を終了するインデックス (0-based, inclusive)
Return values
CORE_STRING_INVALID_ARGUMENT
  • from_ または to_ が不正 (from_ > to_)
  • to_ が src_ の範囲を超えている
CORE_STRING_RUNTIME_ERROR
CORE_STRING_SUCCESS正常に部分文字列のコピーが完了
See also
core_string_buffer_capacity()
core_string_buffer_resize()
core_string_cstr()

◆ core_string_to_i32()

CORE_STRING_ERROR_CODE core_string_to_i32 ( const core_string_t *const string_,
int32_t * out_value_ )

core_string_tオブジェクトの内容をint32_t型整数に変換する。

Note
  • 文字列が整数として解釈できない場合はエラーを返す
  • 変換結果がint32_tの範囲外の場合もエラーを返す

使用例:

core_string_create("1234", &str);
int32_t value = 0;
if (CORE_STRING_SUCCESS == result) {
// value == 1234
}
CORE_STRING_ERROR_CODE core_string_to_i32(const core_string_t *const string_, int32_t *out_value_)
core_string_tオブジェクトの内容をint32_t型整数に変換する。
Definition core_string.c:486
Parameters
[in]string_変換対象の文字列オブジェクト。( core_string_initialization_rule 参照)
[out]out_value_変換結果を格納するポインタ
Return values
CORE_STRING_INVALID_ARGUMENT引数string_またはout_value_がNULL
CORE_STRING_RUNTIME_ERRORstring_がデフォルト状態( core_string_initialization_rule 参照)、空文字列、変換失敗、または範囲外の値
CORE_STRING_SUCCESS正常に変換が完了
See also
strtol()

◆ core_string_trim()

CORE_STRING_ERROR_CODE core_string_trim ( const core_string_t *const src_,
core_string_t *const dst_,
char ltrim_,
char rtrim_ )

指定したトリム文字を取り除いた文字列をdst_にコピーする。

Note
  • 左端の連続するltrim_文字と右端の連続するrtrim_文字を削除する
  • トリム後の文字列長が0になる場合、空文字列としてdst_に格納する
  • 必要に応じてdst_のバッファサイズを再確保する

使用例:

core_string_create(" hello ", &src);
CORE_STRING_ERROR_CODE result = core_string_trim(&src, &dst, ' ', ' ');
if (CORE_STRING_SUCCESS == result) {
// dstには "hello" が格納される
}
CORE_STRING_ERROR_CODE core_string_trim(const core_string_t *const src_, core_string_t *const dst_, char ltrim_, char rtrim_)
指定したトリム文字を取り除いた文字列をdst_にコピーする。
Definition core_string.c:428
Parameters
[in]src_トリム対象の文字列オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
[out]dst_トリム結果の出力先オブジェクト。未初期化状態は不可。( core_string_initialization_rule 参照)
[in]ltrim_左端のトリム対象文字
[in]rtrim_右端のトリム対象文字
Return values
CORE_STRING_INVALID_ARGUMENT引数がNULL
CORE_STRING_RUNTIME_ERRORsrc_がデフォルト状態( core_string_initialization_rule 参照)または長さ取得に失敗
CORE_STRING_MEMORY_ALLOCATE_ERRORメモリ再確保に失敗
CORE_STRING_SUCCESS正常終了
See also
core_string_length()
core_string_substring_copy()
core_string_buffer_reserve()