Better String library
---------------------
by Paul Hsieh
The bstring library is an attempt to provide improved string processing
functionality to the C and C++ language. At the heart of the bstring library
(Bstrlib for short) is the management of "bstring"s which are a significant
improvement over '\0' terminated char buffers.
===============================================================================
Motivation
----------
The standard C string library has serious problems:
1) Its use of '\0' to denote the end of the string means knowing a
string's length is O(n) when it could be O(1).
2) It imposes an interpretation for the character value '\0'.
3) gets() always exposes the application to a buffer overflow.
4) strtok() modifies the string its parsing and thus may not be usable in
programs which are re-entrant or multithreaded.
5) fgets has the unusual semantic of ignoring '\0's that occur before
'\n's are consumed.
6) There is no memory management, and actions performed such as strcpy,
strcat and sprintf are common places for buffer overflows.
7) strncpy() doesn't '\0' terminate the destination in some cases.
8) Passing NULL to C library string functions causes an undefined NULL
pointer access.
9) Parameter aliasing (overlapping, or self-referencing parameters)
within most C library functions has undefined behavior.
10) Many C library string function calls take integer parameters with
restricted legal ranges. Parameters passed outside these ranges are
not typically detected and cause undefined behavior.
So the desire is to create an alternative string library that does not suffer
from the above problems and adds in the following functionality:
1) Incorporate string functionality seen from other languages.
a) MID$() - from BASIC
b) split()/join() - from Python
c) string/char x n - from Perl
2) Implement analogs to functions that combine stream IO and char buffers
without creating a dependency on stream IO functionality.
3) Implement the basic text editor-style functions insert, delete, find,
and replace.
4) Implement reference based sub-string access (as a generalization of
pointer arithmetic.)
5) Implement runtime write protection for strings.
There is also a desire to avoid "API-bloat". So functionality that can be
implemented trivially in other functionality is omitted. So there is no
left$() or right$() or reverse() or anything like that as part of the core
functionality.
Explaining Bstrings
-------------------
A bstring is basically a header which wraps a pointer to a char buffer. Lets
start with the declaration of a struct tagbstring:
struct tagbstring {
int mlen;
int slen;
unsigned char * data;
};
This definition is considered exposed, not opaque (though it is neither
necessary nor recommended that low level maintenance of bstrings be performed
whenever the abstract interfaces are sufficient). The mlen field (usually)
describes a lower bound for the memory allocated for the data field. The
slen field describes the exact length for the bstring. The data field is a
single contiguous buffer of unsigned chars. Note that the existence of a '\0'
character in the unsigned char buffer pointed to by the data field does not
necessarily denote the end of the bstring.
To be a well formed modifiable bstring the mlen field must be at least the
length of the slen field, and slen must be non-negative. Furthermore, the
data field must point to a valid buffer in which access to the first mlen
characters has been acquired. So the minimal check for correctness is:
(slen >= 0 && mlen >= slen && data != NULL)
bstrings returned by bstring functions can be assumed to be either NULL or
satisfy the above property. (When bstrings are only readable, the mlen >=
slen restriction is not required; this is discussed later in this section.)
A bstring itself is just a pointer to a struct tagbstring:
typedef struct tagbstring * bstring;
Note that use of the prefix "tag" in struct tagbstring is required to work
around the inconsistency between C and C++'s struct namespace usage. This
definition is also considered exposed.
Bstrlib basically manages bstrings allocated as a header and an associated
data-buffer. Since the implementation is exposed, they can also be
constructed manually. Functions which mutate bstrings assume that the header
and data buffer have been malloced; the bstring library may perform al_free() or
al_realloc() on both the header and data buffer of any bstring parameter.
Functions which return bstring's create new bstrings. The string memory is
freed by a bdestroy() call (or using the bstrFree macro).
The following related typedef is also provided:
typedef const struct tagbstring * const_bstring;
which is also considered exposed. These are directly bstring compatible (no
casting required) but are just used for parameters which are meant to be
non-mutable. So in general, bstring parameters which are read as input but
not meant to be modified will be declared as const_bstring, and bstring
parameters which may be modified will be declared as bstring. This convention
is recommended for user written functions as well.
Since bstrings maintain interoperability with C library char-buffer style
strings, all functions which modify, update or create bstrings also append a
'\0' character into the position slen + 1. This trailing '\0' character is
not required for bstrings input to the bstring functions; this is provided
solely as a convenience for interoperability with standard C char-buffer
functionality.
Analogs for the ANSI C string library functions have been created when they
are necessary, but have also been left out when they are not. In particular
there are no functions analogous to fwrite, or puts just for the purposes of
bstring. The ->data member of any string is exposed, and therefore can be
used just as easily as char buffers for C functions which read strings.
For those that wish to hand construct bstrings, the following should be kept
in mind:
1) While bstrlib can accept constructed bstrings without terminating
'\0' characters, the rest of the C language string library will not
function properly on such non-terminated strings. This is obvious
but must be kept in mind.
2) If it is intended that a constructed bstring be written to by the
bstring library functions then the data portion should be allocated
by the malloc function and the slen and mlen fields should be entered
properly. The struct tagbstring header is not reallocated, and only
freed by bdestroy.
3) Writing arbitrary '\0' characters at various places in the string
will not modify its length as perceived by the bstring library
functions. In fact, '\0' is a legitimate non-terminating character
for a bstring to contain.
4) For read only parameters, bstring functions do not check the mlen.
I.e., the minimal correctness requirements are reduced to:
(slen >= 0 && data != NULL)
Better pointer arithmetic
-------------------------
One built-in feature of '\0' terminated char * strings, is that its very easy
and fast to obtain a reference to the tail of any string using pointer
arithmetic. Bstrlib does one better by providing a way to get a reference to
any substring of a bstring (or any other length delimited block of memory.)
So rather than just having pointer arithmetic, with bstrlib one essentially
has segment arithmetic. This is achieved using the macro blk2tbstr() which
builds a reference to a block of memory and the macro bmid2tbstr() which
没有合适的资源?快使用搜索试试~ 我知道了~
allegro函数库
共1511个文件
3:712个
c:251个
h:138个
需积分: 9 6 下载量 91 浏览量
2011-02-26
21:18:55
上传
评论
收藏 3.52MB ZIP 举报
温馨提示
可用于进行游戏的开发和维护详情请见c大学教程(电子工业出版社
资源推荐
资源详情
资源评论
收起资源包目录
allegro函数库 (1511个子文件)
ALLEGRO_EVENT.3 11KB
al_set_new_bitmap_flags.3 6KB
al_set_new_display_option.3 5KB
ALLEGRO_PIXEL_FORMAT.3 4KB
al_set_new_display_flags.3 3KB
al_get_standard_path.3 3KB
al_set_blender.3 3KB
al_grab_font_from_bitmap.3 2KB
al_show_native_message_box.3 2KB
al_set_target_bitmap.3 2KB
al_color_name_to_rgb.3 2KB
al_draw_soft_triangle.3 2KB
al_create_native_file_dialog.3 2KB
al_create_audio_stream.3 2KB
ALLEGRO_FS_INTERFACE.3 2KB
ALLEGRO_VERTEX_ELEMENT.3 2KB
ALLEGRO_AUDIO_STREAM.3 2KB
ALLEGRO_FILE_INTERFACE.3 2KB
al_lock_bitmap.3 2KB
al_set_system_mouse_cursor.3 2KB
al_init_user_event_source.3 2KB
al_emit_user_event.3 2KB
al_fixed.3 2KB
al_draw_soft_line.3 2KB
al_reconfigure_joysticks.3 1KB
al_hold_bitmap_drawing.3 1KB
al_load_audio_stream_f.3 1KB
al_fixdiv.3 1KB
al_draw_prim.3 1KB
al_get_audio_stream_fragment.3 1KB
al_fopen.3 1KB
al_draw_bitmap.3 1KB
ALLEGRO_STATE.3 1KB
al_flip_display.3 1KB
al_get_opengl_proc_address.3 1KB
ALLEGRO_LOCKED_REGION.3 1KB
al_calculate_arc.3 1KB
al_iphone_override_screen_scale.3 1KB
al_fixmul.3 1KB
al_get_opengl_extension_list.3 1KB
al_calculate_ribbon.3 1KB
al_get_font_line_height.3 1KB
al_load_ttf_font.3 1KB
al_wait_cond.3 1KB
al_fixsub.3 1KB
al_fixasin.3 1KB
al_fixadd.3 1KB
al_fixatan2.3 1KB
al_play_sample.3 1KB
al_set_sample_instance_pan.3 1KB
al_load_audio_stream.3 1KB
al_cstr.3 1KB
al_store_state.3 1KB
al_draw_indexed_prim.3 1KB
ALLEGRO_PRIM_ATTR.3 1KB
al_reserve_samples.3 1KB
ALLEGRO_PRIM_TYPE.3 1KB
al_is_compatible_bitmap.3 1KB
al_ustr_newf.3 1023B
al_create_sample.3 1021B
al_iphone_program_has_halted.3 1018B
al_open_native_text_log.3 1017B
al_get_backbuffer.3 1014B
al_draw_scaled_rotated_bitmap.3 1012B
al_calculate_spline.3 1009B
al_create_sub_bitmap.3 1000B
al_set_sample.3 989B
al_get_allegro_version.3 987B
al_create_voice.3 973B
al_check_inverse.3 968B
al_itofix.3 965B
al_create_bitmap.3 947B
al_load_sample_f.3 944B
al_ref_ustr.3 936B
ALLEGRO_SAMPLE_INSTANCE.3 935B
ALLEGRO_VERTEX.3 909B
al_ftofix.3 907B
al_create_display.3 884B
al_get_mouse_state.3 879B
al_draw_rotated_bitmap.3 875B
al_draw_pixel.3 870B
al_register_audio_stream_loader_f.3 868B
al_register_audio_stream_loader.3 867B
al_fseek.3 863B
al_fungetc.3 856B
al_attach_audio_stream_to_voice.3 855B
al_fixacos.3 854B
al_get_text_dimensions.3 847B
al_ustr_next.3 847B
al_ustr_prev.3 845B
al_set_config_value.3 839B
al_draw_scaled_bitmap.3 837B
al_draw_text.3 834B
al_fgets.3 832B
al_fixtan.3 832B
al_ref_cstr.3 829B
al_set_current_opengl_context.3 826B
ALLEGRO_MEMORY_INTERFACE.3 812B
al_wait_for_event_until.3 811B
al_lock_mutex.3 809B
共 1511 条
- 1
- 2
- 3
- 4
- 5
- 6
- 16
资源评论
huxuehan123
- 粉丝: 17
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功