mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
c_string.h
Go to the documentation of this file.
1
14
15#ifndef MRBC_SRC_C_STRING_H_
16#define MRBC_SRC_C_STRING_H_
17
18/***** Feature test switches ************************************************/
19/***** System headers *******************************************************/
20//@cond
21#include "vm_config.h"
22#include <stdint.h>
23#include <string.h>
24//@endcond
25
26/***** Local headers ********************************************************/
27#include "value.h"
28#include "vm.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33/***** Constant values ******************************************************/
34#if !defined(MRBC_STRING_SIZE_T)
35#define MRBC_STRING_SIZE_T uint16_t
36#endif
37
38/***** Macros ***************************************************************/
39#define RSTRING_LEN(str) mrbc_string_size(&str)
40#define RSTRING_PTR(str) mrbc_string_cstr(&str)
41
42/***** Typedefs *************************************************************/
43//================================================================
56
57
58/***** Global variables *****************************************************/
59/***** Function prototypes **************************************************/
60//@cond
61mrbc_value mrbc_string_new(mrbc_vm *vm, const void *src, int len);
62mrbc_value mrbc_string_new_alloc(mrbc_vm *vm, void *buf, int len);
65void mrbc_string_clear_vm_id(mrbc_value *str);
67mrbc_value mrbc_string_add(mrbc_vm *vm, const mrbc_value *s1, const mrbc_value *s2);
68int mrbc_string_append(mrbc_value *s1, const mrbc_value *s2);
69int mrbc_string_append_cbuf(mrbc_value *s1, const void *s2, int len2);
70int mrbc_string_index(const mrbc_value *src, const mrbc_value *pattern, int offset);
71int mrbc_string_strip(mrbc_value *src, int mode);
75int mrbc_string_utf8_size(const char *str);
76int mrbc_string_char_size(const char *str, int len);
77int mrbc_string_chars2bytes(mrbc_value *src, int off, int idx);
78int mrbc_string_bytes2chars(const mrbc_value *src, int byte_index);
79//@endcond
80
81
82/***** Inline functions *****************************************************/
83
84//================================================================
91static inline mrbc_value mrbc_string_new_cstr(mrbc_vm *vm, const char *src)
92{
93 return mrbc_string_new(vm, src, (src ? strlen(src) : 0));
94}
95
96
97//================================================================
100static inline int mrbc_string_compare(const mrbc_value *v1, const mrbc_value *v2)
101{
102 int len = (v1->string->size < v2->string->size) ?
103 v1->string->size : v2->string->size;
104
105 int res = memcmp(v1->string->data, v2->string->data, len);
106 if( res != 0 ) return res;
107
108 return v1->string->size - v2->string->size;
109}
110
111//================================================================
114static inline int mrbc_string_size(const mrbc_value *str)
115{
116 return str->string->size;
117}
118
119//================================================================
122static inline char * mrbc_string_cstr(const mrbc_value *v)
123{
124 return (char*)v->string->data;
125}
126
127//================================================================
134static inline int mrbc_string_append_cstr(mrbc_value *s1, const char *s2)
135{
136 return mrbc_string_append_cbuf( s1, s2, strlen(s2) );
137}
138
139#ifdef __cplusplus
140}
141#endif
142#endif
struct RObject mrbc_value
Value object. Default version.
int mrbc_string_index(const mrbc_value *src, const mrbc_value *pattern, int offset)
Definition c_string.c:240
void mrbc_string_delete(mrbc_value *str)
Definition c_string.c:107
mrbc_value mrbc_string_new(mrbc_vm *vm, const void *src, int len)
Definition c_string.c:64
mrbc_value mrbc_string_new_alloc(mrbc_vm *vm, void *buf, int len)
Definition c_string.c:88
void mrbc_string_clear(mrbc_value *str)
Definition c_string.c:118
mrbc_value mrbc_string_dup(mrbc_vm *vm, mrbc_value *s1)
Definition c_string.c:146
mrbc_value mrbc_string_add(mrbc_vm *vm, const mrbc_value *s1, const mrbc_value *s2)
Definition c_string.c:165
int mrbc_string_chomp(mrbc_value *src)
Definition c_string.c:307
int mrbc_string_append_cbuf(mrbc_value *s1, const void *s2, int len2)
Definition c_string.c:213
int mrbc_string_upcase(mrbc_value *str)
Definition c_string.c:337
int mrbc_string_downcase(mrbc_value *str)
Definition c_string.c:359
int mrbc_string_append(mrbc_value *s1, const mrbc_value *s2)
Definition c_string.c:185
int mrbc_string_strip(mrbc_value *src, int mode)
Definition c_string.c:265
struct RString mrbc_string
String object.
static char * mrbc_string_cstr(const mrbc_value *v)
Definition c_string.h:122
static int mrbc_string_compare(const mrbc_value *v1, const mrbc_value *v2)
Definition c_string.h:100
static int mrbc_string_append_cstr(mrbc_value *s1, const char *s2)
Definition c_string.h:134
#define MRBC_STRING_SIZE_T
Definition c_string.h:35
static mrbc_value mrbc_string_new_cstr(mrbc_vm *vm, const char *src)
Definition c_string.h:91
static int mrbc_string_size(const mrbc_value *str)
Definition c_string.h:114
struct RString * string
Definition boxing_no.h:32
String object.
Definition c_string.h:49
MRBC_OBJECT_HEADER
Definition c_string.h:50
uint8_t * data
pointer to allocated buffer.
Definition c_string.h:53
MRBC_STRING_SIZE_T size
string length.
Definition c_string.h:52
mruby/c value definitions
mruby bytecode executor.
struct VM mrbc_vm
Virtual Machine.
Global configuration of mruby/c VM's.