开发者

something like an "extended" C string library?

I have used several dynamically typed languages and I have been avoiding C but enoug开发者_如何学运维h is enough, it's the right tool for the job sometimes and I need to get over it.

The things I miss working with C are associative arrays and large string libraries. Is there a library that gives more options then string.h? Any general advice when it comes to make the transition with strings?

Thanks for reading-Patrick


You can take a look at the Better String Library. The description from the site:

The Better String Library is an abstraction of a string data type which is superior to the C library char buffer string type, or C++'s std::string. Among the features achieved are:

  • Substantial mitigation of buffer overflow/overrun problems and other failures that result from erroneous usage of the common C string library functions
  • Significantly simplified string manipulation
  • High performance interoperability with other source/libraries which expect '\0' terminated char buffers
  • Improved overall performance of common string operations
  • Functional equivalency with other more modern languages

The library is totally stand alone, portable (known to work with gcc/g++, MSVC++, Intel C++, WATCOM C/C++, Turbo C, Borland C++, IBM's native CC compiler on Windows, Linux and Mac OS X), high performance, easy to use and is not part of some other collection of data structures. Even the file I/O functions are totally abstracted (so that other stream-like mechanisms, like sockets, can be used.) Nevertheless, it is adequate as a complete replacement of the C string library for string manipulation in any C program.


POSIX gives you <string.h>, <strings.h> and <regex.h>. If you really need more of a string library than this, C is probably not the right tool for that particular job.

As for a hash table, you can't get a type-safe hash table in C without a lot of nasty macros. If you're OK with just storing void-pointers, or with doing some manual work for each type of map, then you shouldn't be lacking for options. Coding your own hash table is a hoot and a half - just search Stackoverflow for help with the hash function. If you don't want to roll your own, strmap [LGPL] looks decent.


GLib provides many pre-made data structures and string handling functions, but it's a set of functions and types completely separated from the "usual" ones, and it's not a very lightweight dependency.

If instead C++ is a viable alternative for your task, it bundles a string class and several generic containers ready-made into the standard library (and much other related stuff can be found in Boost).


What specifically are you looking for in your extended c-string library?

One way to get better at C, is to create your own c-string library. Then make it open source, and let others help refine it.

I don't usually advocate creating your own string libaries, but w.r.t. C, it's a great way to learn C.


Much of the power of C consists of the ability to have direct control over the memory as a sequence of bytes. It is a bit against the philosophy of the language to treat strings as something higher-level than that.

I would recommend rolling your own very basic one. It will be an enlightening experience especially to learn pointer arithmetics and loops.

For example, learn about "Schlemiel the Painter's algorithm" regarding strcat and design your library to solve this problem.


I've not used it myself, but you should at least review the SEI/CERT library Specifications for Managed Strings, 2nd Edition. The code can be found at CERT.


An associative array associating string keys and struct values in C consists of:

  • A hash function for strings
  • An array with a prime number of elements, inside each of which is a linked-list head.
  • Linked-list elements containing char * pointers to the stored keys and (optionally) a struct * pointer to the corresponding value for each key.

To store a string key in your associative array:

  • Hash it modulo that prime array size.
  • In that array bin, add it to the linked-list.
  • Assign the value pointer to the value you are adding.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜