The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

strings.c File Reference

#include "strings.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

Include dependency graph for strings.c:

Include dependency graph

Go to the source code of this file.

Defines

#define JAM_STRING_MAGIC   ((char)0xcf)
#define JAM_STRING_MAGIC_SIZE   4

Functions

void assert_invariants (string *self)
void extend_full (string *self, char *start, char *finish)
void string_append (string *self, char *rhs)
void string_append_range (string *self, char *start, char *finish)
char string_back (string *self)
void string_copy (string *s, char *rhs)
void string_free (string *s)
void string_new (string *s)
void string_pop_back (string *self)
void string_push_back (string *self, char x)
void string_reserve (string *self, size_t capacity)
void string_reserve_internal (string *self, size_t capacity)
void string_truncate (string *self, size_t n)
void string_unit_test ()


Define Documentation

#define JAM_STRING_MAGIC   ((char)0xcf)
 

Definition at line 9 of file strings.c.

Referenced by assert_invariants(), and string_new().

#define JAM_STRING_MAGIC_SIZE   4
 

Definition at line 10 of file strings.c.

Referenced by string_reserve_internal().


Function Documentation

void assert_invariants string self  )  [static]
 

Definition at line 11 of file strings.c.

References string::capacity, i, JAM_STRING_MAGIC, string::magic, string::opt, string::size, and string::value.

Referenced by string_append(), string_append_range(), string_back(), string_free(), string_new(), string_reserve(), and string_truncate().

00012 {
00013     int i;
00014     
00015     assert( self->size < self->capacity );
00016     assert( ( self->capacity <= sizeof(self->opt) ) == ( self->value == self->opt ) );
00017     assert( strlen( self->value ) == self->size );
00018 
00019     for (i = 0; i < 4; ++i)
00020     {
00021         assert( self->magic[i] == JAM_STRING_MAGIC );
00022         assert( self->value[self->capacity + i] == JAM_STRING_MAGIC );
00023     }
00024 }

void extend_full string self,
char *  start,
char *  finish
[static]
 

Definition at line 77 of file strings.c.

References string::capacity, string::size, string_reserve_internal(), and string::value.

Referenced by string_append(), and string_append_range().

00078 {
00079     size_t new_size = self->capacity + ( finish - start );
00080     size_t new_capacity = self->capacity;
00081     size_t old_size = self->capacity;
00082     while ( new_capacity < new_size + 1)
00083         new_capacity <<= 1;
00084     string_reserve_internal( self, new_capacity );
00085     memcpy( self->value + old_size, start, new_size - old_size );
00086     self->value[new_size] = 0;
00087     self->size = new_size;
00088 }

Here is the call graph for this function:

void string_append string self,
char *  rhs
 

Definition at line 90 of file strings.c.

References assert_invariants(), string::capacity, end, extend_full(), p, string::size, and string::value.

Referenced by bindmodule(), builtin_normalize_path(), class_module_name(), import_base_rule(), new_module_str(), string_copy(), and var_expand().

00091 {
00092     char* p = self->value + self->size;
00093     char* end = self->value + self->capacity;
00094     assert_invariants( self );
00095     
00096     while ( *rhs && p != end)
00097         *p++ = *rhs++;
00098     
00099     if ( p != end )
00100     {
00101         *p = 0;
00102         self->size = p - self->value;
00103     }
00104     else
00105     {
00106         extend_full( self, rhs, rhs + strlen(rhs) );
00107     }
00108     assert_invariants( self );
00109 }

Here is the call graph for this function:

void string_append_range string self,
char *  start,
char *  finish
 

Definition at line 111 of file strings.c.

References assert_invariants(), string::capacity, end, extend_full(), p, string::size, and string::value.

Referenced by builtin_match(), file_build1(), path_build(), string_push_back(), var_defines(), and var_expand().

00112 {
00113     char* p = self->value + self->size;
00114     char* end = self->value + self->capacity;
00115     assert_invariants( self );
00116     
00117     while ( p != end && start != finish )
00118         *p++ = *start++;
00119     
00120     if ( p != end )
00121     {
00122         *p = 0;
00123         self->size = p - self->value;
00124     }
00125     else
00126     {
00127         extend_full( self, start, finish );
00128     }
00129     assert_invariants( self );
00130 }

Here is the call graph for this function:

char string_back string self  ) 
 

Definition at line 156 of file strings.c.

References assert_invariants(), and string::value.

00157 {
00158     assert_invariants( self );
00159     return self->value[self->size - 1];
00160 }

Here is the call graph for this function:

void string_copy string s,
char *  rhs
 

Definition at line 132 of file strings.c.

References s, string_append(), and string_new().

Referenced by builtin_caller_module(), builtin_normalize_path(), downcase_list(), new_module_str(), timestamp(), var_edit_shift(), and var_expand().

00133 {
00134     string_new( s );
00135     string_append( s, rhs );
00136 }

Here is the call graph for this function:

void string_free string s  ) 
 

Definition at line 42 of file strings.c.

References assert_invariants(), string::opt, s, and string::value.

Referenced by bindmodule(), builtin_caller_module(), builtin_glob_back(), builtin_match(), builtin_normalize_path(), class_module_name(), downcase_list(), file_dirscan(), if(), import_base_rule(), new_module_str(), search(), search_for_target(), string_unit_test(), timestamp(), var_defines(), var_edit_shift(), and var_expand().

00043 {
00044     assert_invariants( s );
00045     if ( s->value != s->opt )
00046         free( s->value );
00047 }

Here is the call graph for this function:

void string_new string s  ) 
 

Definition at line 30 of file strings.c.

References assert_invariants(), string::capacity, JAM_STRING_MAGIC, string::magic, string::opt, s, string::size, and string::value.

Referenced by bindmodule(), builtin_glob_back(), builtin_match(), builtin_normalize_path(), class_module_name(), downcase_list(), file_dirscan(), import_base_rule(), search(), search_for_target(), string_copy(), string_unit_test(), timestamp(), var_defines(), and var_expand().

00031 {
00032     s->value = s->opt;
00033     s->size = 0;
00034     s->capacity = sizeof(s->opt);
00035     s->opt[0] = 0;
00036 #ifndef NDEBUG
00037     memset(s->magic, JAM_STRING_MAGIC, sizeof(s->magic));
00038 #endif
00039     assert_invariants( s );
00040 }

Here is the call graph for this function:

void string_pop_back string self  ) 
 

Definition at line 146 of file strings.c.

References string::size, and string_truncate().

Referenced by builtin_caller_module().

00147 {
00148     string_truncate( self, self->size - 1 );
00149 }

Here is the call graph for this function:

void string_push_back string self,
char  x
 

Definition at line 151 of file strings.c.

References string_append_range().

Referenced by bindmodule(), builtin_normalize_path(), file_build1(), import_base_rule(), path_build(), and string_unit_test().

00152 {
00153     string_append_range( self, &x, &x + 1 );
00154 }

Here is the call graph for this function:

void string_reserve string self,
size_t  capacity
 

Definition at line 68 of file strings.c.

References assert_invariants(), and string_reserve_internal().

00069 {
00070     assert_invariants( self );
00071     if ( capacity <= self->capacity )
00072         return;
00073     string_reserve_internal( self, capacity );
00074     assert_invariants( self );
00075 }

Here is the call graph for this function:

void string_reserve_internal string self,
size_t  capacity
[static]
 

Definition at line 49 of file strings.c.

References string::capacity, JAM_STRING_MAGIC_SIZE, string::magic, string::opt, and string::value.

Referenced by extend_full(), and string_reserve().

00050 {
00051     if ( self->value == self->opt )
00052     {
00053         self->value = (char*)malloc( capacity + JAM_STRING_MAGIC_SIZE );
00054         self->value[0] = 0;
00055         strncat( self->value, self->opt, sizeof(self->opt) );
00056         assert( strlen( self->value ) <= self->capacity ); /* This is a regression test */
00057     }
00058     else
00059     {
00060         self->value = (char*)realloc( self->value, capacity + JAM_STRING_MAGIC_SIZE );
00061     }
00062 #ifndef NDEBUG
00063     memcpy( self->value + capacity, self->magic, JAM_STRING_MAGIC_SIZE );
00064 #endif
00065     self->capacity = capacity;
00066 }

void string_truncate string self,
size_t  n
 

Definition at line 138 of file strings.c.

References assert_invariants(), and string::value.

Referenced by builtin_match(), file_dirscan(), search(), search_for_target(), string_pop_back(), timestamp(), var_defines(), var_expand(), and while().

00139 {
00140     assert_invariants( self );
00141     assert( n <= self->capacity );
00142     self->value[self->size = n] = 0;
00143     assert_invariants( self );
00144 }

Here is the call graph for this function:

void string_unit_test  ) 
 

Definition at line 163 of file strings.c.

References i, string::opt, s, string_free(), string_new(), string_push_back(), and string::value.

Referenced by run_unit_tests().

00164 {
00165     string s[1];
00166     int i;
00167     char buffer[sizeof(s->opt) * 2 + 2];
00168     int limit = sizeof(buffer) > 254 ? 254 : sizeof(buffer);
00169 
00170     string_new(s);
00171     
00172     for (i = 0; i < limit; ++i)
00173     {
00174         string_push_back( s, (char)(i + 1) );
00175     };
00176 
00177     for (i = 0; i < limit; ++i)
00178     {
00179         assert( i < s->size );
00180         assert( s->value[i] == (char)(i + 1));
00181     }
00182 
00183     string_free(s);
00184     
00185 }

Here is the call graph for this function:


Generated on Mon Nov 8 17:08:25 2004 for MPT by  doxygen 1.3.9.1