• This repository has been archived on 11/Jun/2020
  • Stars
    star
    351
  • Rank 120,906 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple C string lib

cstring 库

C 语言没有原生的 string 类型,这使得 string 的管理非常麻烦。cstring 是一个简单的 string 库,它主要解决以下几个问题:

  • 对于短字符串(小于 32 字节),进行 string interning 。这可以在文本处理时节约不少内存。短 string 相当于 symbol 类型,对它做比较操作的代价可以减少到 O(1) 。
  • 对于临时字符串,如果长度不大(小于 128 字节),尽可能的放在 stack 上,避免动态内存分配。
  • 支持常量字符串,对于常量短字符串只做一次 string interning 操作。
  • 使用引用计数管理相同的字符串,减少字符串的拷贝。
  • 短字符串,常量字符串,以及引用次数非常多(大于 64K 次)的字符串可以不动态释放,简化生命期管理。
  • 惰性计算,以及缓存字符串的 hash 值,以方便实现 hashmap 。
  • 这个库是线程安全的。

cstring_buffer

不要直接定义 cstring_buffer 类型,而应该用 CSTRING_BUFFER(var) 声明,它相当于声明了一个名为 var 的 cstring_buffer 对象。

cstring_buffer 位于栈上,通常不需要回收。但是在函数结束时,应该使用 CSTRING_CLOSE(var) 关闭它。

新声明的 cstring_buffer 对象是一个空字符串,可以用下面两个 api 修改它。

cstring cstring_cat(cstring_buffer sb, const char * str);
cstring cstring_printf(cstring_buffer sb, const char * format, ...);

cstring

如果需要把字符串做参数传递,就应该使用 cstring 类型,而不是 cstring_buffer 类型。CSTRING(var) 可以把 var 这个 cstring_buffer 对象,转换为 cstring 类型。

但是,在对 cstring_buffer 对象做新的操作后,这个 cstring 可能无效。所以每次传递 cstring_buffer 内的值,最好都重新用 CSTRING 宏取一次。

函数调用的参数以及返回值,都应该使用 cstring 类型。如果 cstring 是由外部传入的,无法确定它的数据在栈上还是堆上,所以不能长期持有。如果需要把 cstring 保存在数据结构中,可以使用这对 API :

cstring cstring_grab(cstring s);
void cstring_release(cstring s);

把 cstring 转化为标准的 const char * ,只需要用 s->cstr 即可。

cstring 的比较操作以及 hash 操作都比 const char * 廉价,所以,请使用以下 API :

int cstring_equal(cstring a, cstring b);
uint32_t cstring_hash(cstring s);

literal

CSTRING_LITERAL(var, literal) 可以声明一个常量 cstring 。这里 literal 必须是一个 " 引起的字符串常量。

More Repositories

1

skynet

A lightweight online game framework
C
12,707
star
2

coroutine

A asymmetric coroutine library for C.
C
2,381
star
3

pbc

A protocol buffers library for C
C
1,607
star
4

mptun

Multi-path Tunnel
C
1,243
star
5

lua53doc

The Chinese Translation of Lua 5.3 document
HTML
916
star
6

sproto

Yet another protocol library like google protocol buffers , but simple and fast.
C
912
star
7

lua-snapshot

Make a snapshot for lua state to detect memory leaks.
C
497
star
8

rudp

Reliable UDP
C
395
star
9

stellaris_cn

Stellaris 群星 汉化 Mod
Lua
386
star
10

buddy

Buddy memory allocation
C
316
star
11

aoi

Area of Interest Library
C
293
star
12

luaecs

C
283
star
13

ltask

C
257
star
14

neuralnet

Toy neural network
C
223
star
15

skynet_sample

A sample for skynet
Lua
219
star
16

lua-bgfx

Yet another bgfx lua binding
Lua
215
star
17

socket-server

C
209
star
18

hive

Parallel lua states
C
202
star
19

mread

Multi-Read from one bind port
C
195
star
20

windsoul

An open source 3d engine
C
160
star
21

luareload

reload a lua module
Lua
147
star
22

lua-serialize

Serialize lua objects into a binary block
C
140
star
23

lua-trace

Trace for debug lua
Lua
135
star
24

stable-connection

C
122
star
25

luadebug

A lua debugger in independent lua VM
C
118
star
26

tpp_feedback

《程序员修炼之道》第二版中译反馈
112
star
27

lua-int64

A int64 lib for lua with lightuserdata in 64bit architecture
C
109
star
28

sharplua

Embed lua 5.3 into mono
C
108
star
29

math3d

A lua math lib for linear algebra (matrix and vector)
C
102
star
30

lua-bson

A BSON library for lua
C
102
star
31

ldebug

A Lua Remote Debugger
Lua
94
star
32

battlearena

A experimental project for moba like game server
Lua
87
star
33

luaprofiler

simple lua profiler
C
86
star
34

xlsx2txt

Convert xlsx file to txt for grep text
C
81
star
35

datalist

A simple list data sheet
C
79
star
36

lua-mongo

A simple lua mongo driver
C
76
star
37

remotepvrtool

C
71
star
38

ameba

multithread for lua 5.2
67
star
39

bpa

A bump pointer allocator
C
66
star
40

lua-db

A database shared data among multi-states .
C
62
star
41

lsocket

lsocket from http://tset.de/lsocket/
C
59
star
42

skynet_package

C
58
star
43

luabind

simple and robust lua binding lib
C
56
star
44

lua-stable

Share table among lua states
C
55
star
45

ejoy3d

Just a toy
C
55
star
46

efkbgfx

A bgfx renderer for effekseer runtime
Scala
50
star
47

luawinfile

Replacement lfs and lua original api to support utf-8 filename
C
50
star
48

luacc

LUACC allows you write C code in lua
Lua
47
star
49

lua-conf

Convert a lua table to a C object , and share it among many lua states .
C
46
star
50

xenonauts

xenonauts chinese translation
Lua
45
star
51

rogue

A simple rogue lib for lua
C
44
star
52

dfont

Manage texture for Chinese character rendering.
C
44
star
53

lua-array

lua sparse array
C
43
star
54

handlemap

Map object pointer to an unique id
C
42
star
55

atomdict

A data structure for data exchange between multi lua states.
C
38
star
56

skynet-reload

Reload skynet lua service
C
37
star
57

simplethread

A simple thread library base on pthread and win thread
C
36
star
58

iupmingw

IUP all in one
Lua
36
star
59

vla

Variable length array
C
35
star
60

skynet_group

示范如何让多组游戏服务器运行在同一个 skynet 进程内
Lua
35
star
61

backtrace-mingw

Automatically exported from code.google.com/p/backtrace-mingw
C
33
star
62

syncmodel

一个根据时间戳同步的模型
Lua
31
star
63

tracedoc

Lua
30
star
64

roguestale_cn

A Chinese translation mod of Rogue's Tale
C
30
star
65

bgfxidl

An IDL for bgfx
Lua
28
star
66

attrib

Expression calculate machine
C
28
star
67

luaxlsx

C
27
star
68

pathfinding

A simple A star path-finding module for Lua
C
26
star
69

lalloc

A allocator for lua
C
26
star
70

eaccodec

A simple Ericsson Texture EAC codec
C
25
star
71

lua-crypt

simple crypt lib
C
24
star
72

aura

C
24
star
73

skynet2

单独放一些临时代码, 之后会合并到 skynet 的 2.0 分支中
C
23
star
74

lthread

lua thread (step execute a lua function)
C
21
star
75

skynet_transaction

A example of transaction
Lua
20
star
76

caccessor

Let lua access C types
C
20
star
77

lmonitor

monitor lua calls
C
19
star
78

font16xaa

16xAA font rendering using coverage masks
C
19
star
79

stringid

C
18
star
80

luatypesystem

A simple type system for lua
Lua
17
star
81

manualgc

Automatically exported from code.google.com/p/manualgc
C
17
star
82

ctable

Dump a lua table into a block of memory, and use it as a lua table later.
Lua
17
star
83

syncobj

Lua
15
star
84

tablepointer

Convert lua table to pointer, and iterate it.
C
15
star
85

lua_typecheck

Lua
14
star
86

datatree

C
13
star
87

netout

Redirect stdout to a tcp socket
C
13
star
88

textcell

ImGui style table cell, but for text mode
C
13
star
89

skynet-demo

A demo for skynet (WIP)
Lua
12
star
90

bgfxlshaderc

A lua version of bgfx shaderc
Lua
12
star
91

pdxparser

Paradox Game data file parser
Lua
10
star
92

etccompose

compose ETC2_EAC without encode
C
10
star
93

kdtree

C
9
star
94

freeabc

给自己用的 ABC 输入法
Lua
9
star
95

stylecache

C
9
star
96

luaproto

dump lua prototype constants
C
7
star
97

routemap

C
6
star
98

treeclone

Clone a tree by blueprint
C
5
star
99

cmod

A simple C modules manager
C
5
star
100

extable

Put data into external lua state
C
5
star