You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by vi...@apache.org on 2008/02/19 20:43:39 UTC
svn commit: r629204 - in /stdcxx/trunk/tests: include/rw_braceexp.h
src/braceexp.cpp
Author: vitek
Date: Tue Feb 19 11:43:36 2008
New Revision: 629204
URL: http://svn.apache.org/viewvc?rev=629204&view=rev
Log:
2008-02-19 Travis Vitek <vi...@roguewave.com>
* tests/src/braceexp.cpp: Use unsigned values where appropriate
Use change type of seperator to char instead of int. Inlines a
few small functions. General cleanup.
* tests/include/rw_braceexp.h: Ditto. Add documentation for
rw_brace_expand().
Modified:
stdcxx/trunk/tests/include/rw_braceexp.h
stdcxx/trunk/tests/src/braceexp.cpp
Modified: stdcxx/trunk/tests/include/rw_braceexp.h
URL: http://svn.apache.org/viewvc/stdcxx/trunk/tests/include/rw_braceexp.h?rev=629204&r1=629203&r2=629204&view=diff
==============================================================================
--- stdcxx/trunk/tests/include/rw_braceexp.h (original)
+++ stdcxx/trunk/tests/include/rw_braceexp.h Tue Feb 19 11:43:36 2008
@@ -33,10 +33,25 @@
// rw_brace_expand() performs brace expansion according to chapter 3.5.1 of
// the Bash Reference Manual with the exception of special handling for the
-// string `${'.
+// string `${'. This function is, for the most part, consistent with brace
+// expansion used by bash 3.0.
+//
+// this function will attempt to expand the brace expression `brace_expr'
+// into `n' bytes of the output buffer `s', seperating each expansion with
+// a single seperator character `sep'. if the output buffer `s' is is null,
+// or the number of bytes `n' is insufficient to contain all expansions of
+// `brace_expr', an appropriately sized buffer will be allocated with
+// malloc(). a pointer to the output buffer that is used will be returned.
+// if the pointer returned is not equal to the user supplied input buffer
+// `s', then the caller is expected to free() the returned pointer at some
+// point.
+//
+// this function can return null if the brace expansion could not be
+// processed.
+//
_TEST_EXPORT char*
-rw_brace_expand (const char* brace_expr, char* s, int n, int sep);
+rw_brace_expand (const char* brace_expr, char* s, _RWSTD_SIZE_T n, char sep);
#endif // RW_BRACEEXP_H_INCLUDED
Modified: stdcxx/trunk/tests/src/braceexp.cpp
URL: http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/braceexp.cpp?rev=629204&r1=629203&r2=629204&view=diff
==============================================================================
--- stdcxx/trunk/tests/src/braceexp.cpp (original)
+++ stdcxx/trunk/tests/src/braceexp.cpp Tue Feb 19 11:43:36 2008
@@ -2,17 +2,17 @@
#include <string.h>
-static int _rw_is_digit (int ch)
+inline int _rw_is_digit (int ch)
{
return !(ch < '0' || '9' < ch);
}
-static int _rw_is_lower (int ch)
+inline int _rw_is_lower (int ch)
{
return !(ch < 'a' || 'z' < ch);
}
-static int _rw_is_upper (int ch)
+inline int _rw_is_upper (int ch)
{
return !(ch < 'A' || 'Z' < ch);
}
@@ -25,8 +25,8 @@
{
bool is_escaped = false;
- for (/**/; *beg; ++beg)
- {
+ for (/**/; *beg; ++beg) {
+
if (end && !(beg < end))
break;
@@ -50,8 +50,8 @@
bool is_escaped = false;
// nested brace depth
- for (int depth = 1; *beg; ++beg)
- {
+ for (int depth = 1; *beg; ++beg) {
+
if (end && !(beg < end))
break;
@@ -83,8 +83,8 @@
bool is_escaped = false;
// nested brace depth
- for (int depth = 0; *beg; ++beg)
- {
+ for (int depth = 0; *beg; ++beg) {
+
if (end && !(beg < end))
break;
@@ -124,17 +124,21 @@
// expand `brace_expr' into `len' bytes of `buf'. if it won't fit, we
// allocate a buffer with malloc() and return that. so the user needs
// to free() the return buffer if it is not equal to `buf'.
- char* _C_build_and_expand (const char* brace_expr,
- char* buf, int len, int sep);
+ char* build_and_expand (const char* brace_expr,
+ char* buf, _RWSTD_SIZE_T len, char sep);
private:
+ // not implemented
+ _rw_brace_graph (const _rw_brace_graph&);
+ _rw_brace_graph& operator= (const _rw_brace_graph&);
+
// node for a directed-acyclic-graph that we build from the original
// brace expression
struct _rw_brace_node
{
const char* str_;
- int len_;
+ _RWSTD_SIZE_T len_;
_rw_brace_node* sibling_;
_rw_brace_node* child_;
@@ -142,55 +146,55 @@
// retrieve a new node. nodes are allocated in large blocks. those
// blocks are deallocated when this graph instance is destroyed.
- _rw_brace_node* _C_get_new_node ();
+ _rw_brace_node* get_new_node ();
// this function generates a dag from an arbitrary brace expression.
// the format is `prefix{body}suffix'. prefix, and suffix can both
// be the empty string. body may be a comma seperated list of tokens,
// a range of tokens, or arbitrary literal text. escapes on commas
// and braces are ignored, and left intact otherwise.
- _rw_brace_node* _C_build_anything (const char* beg, const char* end);
+ _rw_brace_node* build_anything (const char* beg, const char* end);
// generates a dag from an arbitrary range brace expression. the
// format is `{?..?}suffix' where both ? are alphanumeric digits
// of the same character class.
- _rw_brace_node* _C_build_range (const char* beg, const char* end);
+ _rw_brace_node* build_range (const char* beg, const char* end);
// generates a dag from an arbitrary list brace expression. the
// format is `{a,b[,c]}suffix', where `a', `b' and `c' are full
- // brace expansions that would be processed by _C_build_anything.
- _rw_brace_node* _C_build_list (const char* beg, const char* end);
+ // brace expansions that would be processed by build_anything.
+ _rw_brace_node* build_list (const char* beg, const char* end);
// generates a dag from literal text. no brace expansion is done
// on the literal text.
- _rw_brace_node* _C_build_literal (const char* beg, const char* end);
+ _rw_brace_node* build_literal (const char* beg, const char* end);
// the number of nodes held by each brace buffer [see below]
- enum { _C_size = 64 };
+ enum { size = 64 };
// buffer to avoid many small allocations
struct _rw_brace_buffer
{
- _rw_brace_node nodes_ [_C_size];
+ _rw_brace_node nodes_ [size];
_rw_brace_buffer* next_;
};
// the initial set of nodes is preallocated as part of this graph
// object instance. additional blocks of nodes will be allocated
- // as is necessary by the _C_get_new_node() member.
+ // as is necessary by the get_new_node() member.
_rw_brace_buffer node_cache_;
- int used_; // number of nodes used in the last buffer
+ _RWSTD_SIZE_T used_; // number of nodes used in the last buffer
_rw_brace_buffer* nodes_; // pointer to last allocated node buffer
// code for generating the string
- bool _C_append (const char* beg, int n);
+ bool append (const char* beg, _RWSTD_SIZE_T n);
char* user_buf_; // this is the buffer
char* string_buf_;
- int string_cap_;
- int string_len_;
+ _RWSTD_SIZE_T string_cap_;
+ _RWSTD_SIZE_T string_len_;
// we use this to walk the stack. we need to walk from the root
// of the tree to each leaf. when we reach a leaf, we need a way
@@ -215,11 +219,11 @@
// this function walks the dag, leaving a trail of context
// objects so we can walk back to the root and write the data
// at each level in the graph.
- bool _C_brace_expand (_rw_recursion_context* self, char sep);
+ bool brace_expand (_rw_recursion_context* self, char sep);
// this function writes the data at each level of the dag
// to our internal buffer.
- bool _C_brace_expand_write (_rw_recursion_context* context);
+ bool brace_expand_write (_rw_recursion_context* context);
};
_rw_brace_graph::_rw_brace_graph ()
@@ -252,13 +256,13 @@
char*
-_rw_brace_graph::_C_build_and_expand (const char* brace_expr,
- char* buf, int len, int sep)
+_rw_brace_graph::build_and_expand (const char* brace_expr,
+ char* buf, _RWSTD_SIZE_T len, char sep)
{
- const int brace_expr_len = strlen (brace_expr);
+ const _RWSTD_SIZE_T brace_expr_len = strlen (brace_expr);
- _rw_brace_node* root = _C_build_anything (brace_expr,
- brace_expr + brace_expr_len);
+ _rw_brace_node* root = build_anything (brace_expr,
+ brace_expr + brace_expr_len);
if (!root)
return 0;
@@ -271,7 +275,7 @@
// this helps us do the building of the output string
_rw_recursion_context context (root);
- if (!_C_brace_expand (&context, sep))
+ if (!brace_expand (&context, sep))
{
if (string_buf_ != user_buf_)
free (string_buf_);
@@ -281,7 +285,7 @@
// kill the last seperator with a null terminator
else if (string_buf_)
{
- const int pos = string_len_ < 1 ? 0 : string_len_ - 1;
+ const _RWSTD_SIZE_T pos = string_len_ < 1 ? 0 : string_len_ - 1;
string_buf_ [pos] = '\0';
}
@@ -289,12 +293,12 @@
}
_rw_brace_graph::_rw_brace_node*
-_rw_brace_graph::_C_get_new_node ()
+_rw_brace_graph::get_new_node ()
{
used_ += 1;
// if we run out of space, grab a new block
- if (! (used_ < _C_size))
+ if (! (used_ < size))
{
nodes_->next_
= (_rw_brace_buffer*)malloc (sizeof (_rw_brace_buffer));
@@ -304,7 +308,7 @@
nodes_ = nodes_->next_;
nodes_->next_ = 0;
- used_ -= _C_size;
+ used_ -= size;
}
_rw_brace_node* node = &nodes_->nodes_ [used_ - 1];
@@ -317,29 +321,29 @@
}
_rw_brace_graph::_rw_brace_node*
-_rw_brace_graph::_C_build_anything (const char* beg, const char* end)
+_rw_brace_graph::build_anything (const char* beg, const char* end)
{
//
const char* open_brace = _rw_find_open_brace (beg, end);
if (open_brace)
{
// build a node for the prefix if there is one
- _rw_brace_node* prefix = _C_get_new_node ();
+ _rw_brace_node* prefix = get_new_node ();
prefix->str_ = beg;
prefix->len_ = (open_brace - beg);
// try to build a range expansion
_rw_brace_node* child = 0;
- child = _C_build_range (open_brace, end);
+ child = build_range (open_brace, end);
if (!child) {
// try to do a list expansion
- child = _C_build_list (open_brace, end);
+ child = build_list (open_brace, end);
if (!child) {
// try to do a literal expansion
- child = _C_build_literal (open_brace, end);
+ child = build_literal (open_brace, end);
if (!child) {
return 0;
}
@@ -354,7 +358,7 @@
// there was no open brace, so the entire text from beg to end
// is a literal
- _rw_brace_node* prefix = _C_get_new_node ();
+ _rw_brace_node* prefix = get_new_node ();
if (!prefix)
return 0;
@@ -365,9 +369,9 @@
}
_rw_brace_graph::_rw_brace_node*
-_rw_brace_graph::_C_build_literal (const char* beg, const char* end)
+_rw_brace_graph::build_literal (const char* beg, const char* end)
{
- _rw_brace_node* prefix = _C_get_new_node ();
+ _rw_brace_node* prefix = get_new_node ();
if (!prefix)
return 0;
@@ -378,7 +382,7 @@
}
_rw_brace_graph::_rw_brace_node*
-_rw_brace_graph::_C_build_range (const char* beg, const char* end)
+_rw_brace_graph::build_range (const char* beg, const char* end)
{
// check for {a..z} type range expression. make sure not to
// reference past the end of the string.
@@ -416,7 +420,7 @@
const int dir = (cbeg < cend) ? 1 : -1;
// build the suffix
- _rw_brace_node* suffix = _C_build_anything (beg + 6, end);
+ _rw_brace_node* suffix = build_anything (beg + 6, end);
if (!suffix)
return false; // failed to parse suffix
@@ -428,7 +432,7 @@
// create a child from whatever is between beg and end. that childs
// next pointer will be the suffix we created above.
- _rw_brace_node* child = _C_get_new_node ();
+ _rw_brace_node* child = get_new_node ();
if (!child) {
return false;
}
@@ -452,7 +456,7 @@
}
// create the last node in the range.
- _rw_brace_node* child = _C_get_new_node ();
+ _rw_brace_node* child = get_new_node ();
if (!child) {
return false;
}
@@ -477,7 +481,7 @@
}
_rw_brace_graph::_rw_brace_node*
-_rw_brace_graph::_C_build_list (const char* beg, const char* end)
+_rw_brace_graph::build_list (const char* beg, const char* end)
{
//
// {abc,d{1..3}e}f
@@ -526,7 +530,7 @@
return false; // no list terminator
// build a node for the suffix.
- _rw_brace_node* suffix = _C_build_anything (eol+1, end);
+ _rw_brace_node* suffix = build_anything (eol+1, end);
if (!suffix)
return false; // failed to parse end
@@ -537,7 +541,7 @@
// create a child from whatever is between beg and end. that childs
// next pointer will be the suffix we created above.
- _rw_brace_node* child = _C_build_anything (beg, mid);
+ _rw_brace_node* child = build_anything (beg, mid);
if (!child) {
return false;
}
@@ -569,7 +573,7 @@
// beg eol end
// build nodes from the last entry in the list
- _rw_brace_node* child = _C_build_anything (beg, eol);
+ _rw_brace_node* child = build_anything (beg, eol);
if (!child) {
return false;
}
@@ -587,15 +591,15 @@
return first_child;
}
-bool _rw_brace_graph::_C_append (const char* s, int n)
+bool _rw_brace_graph::append (const char* s, _RWSTD_SIZE_T n)
{
- const int new_len = string_len_ + n;
+ const _RWSTD_SIZE_T new_len = string_len_ + n;
// not enough space, grow buf
if (! (new_len < string_cap_)) {
// buf grows in 256 byte blocks
- int new_cap = string_cap_;
+ _RWSTD_SIZE_T new_cap = string_cap_;
while (! (new_len < new_cap))
new_cap += 256;
@@ -632,24 +636,24 @@
}
-bool _rw_brace_graph::_C_brace_expand_write (_rw_recursion_context* context)
+bool _rw_brace_graph::brace_expand_write (_rw_recursion_context* context)
{
if (context->parent_) {
- if (!_C_brace_expand_write (context->parent_))
+ if (!brace_expand_write (context->parent_))
return false;
}
// write the data at this level
- //return _C_append (context->node_->str_, context->node_->len_);
+ //return append (context->node_->str_, context->node_->len_);
bool is_escaped = false;
const char* beg = context->node_->str_;
- for (int n = 0; n < context->node_->len_; ++n, ++beg) {
+ for (_RWSTD_SIZE_T n = 0; n < context->node_->len_; ++n, ++beg) {
is_escaped = !is_escaped && (*beg == '\\');
if (!is_escaped) {
- if (!_C_append (beg, 1))
+ if (!append (beg, 1))
return false;
}
}
@@ -657,24 +661,24 @@
return true;
}
-bool _rw_brace_graph::_C_brace_expand (_rw_recursion_context* self, char sep)
+bool _rw_brace_graph::brace_expand (_rw_recursion_context* self, char sep)
{
// if this node has no children or siblings, we have found a full
// expansion.
if (!self->node_ ||
!self->node_->sibling_ && !self->node_->child_) {
- const int length_before = string_len_;
+ const _RWSTD_SIZE_T length_before = string_len_;
// use recursion again to walk back to the root the graph and
// write each contexts data as we unwind back toward the leaf
- if (!_C_brace_expand_write (self))
+ if (!brace_expand_write (self))
return false;
- const int length_after = string_len_;
+ const _RWSTD_SIZE_T length_after = string_len_;
// don't write a seperator if we wrote no data
- if (length_before != length_after && !_C_append (&sep, 1))
+ if (length_before != length_after && !append (&sep, 1))
return false;
return true;
@@ -685,7 +689,7 @@
_rw_recursion_context context (self);
while (context.node_) {
- if (!_C_brace_expand (&context, sep))
+ if (!brace_expand (&context, sep))
return false;
context.node_ = context.node_->sibling_;
@@ -695,12 +699,8 @@
}
//
-// returns 0 if brace_expr is not a valid brace expression, else a
-// pointer to the buf that stores the expanded expression. if the
-// returned value is not the same pointer as the input parameter
-// expanded, then the user must deallocate the pointer with a call
-// to free().
-char* rw_brace_expand (const char* brace_expr, char* s, int n, int sep)
+char* rw_brace_expand (const char* brace_expr,
+ char* s, _RWSTD_SIZE_T n, char sep)
{
if (!brace_expr)
return 0;
@@ -708,7 +708,7 @@
_rw_brace_graph graph;
// build the graph, and then expand it into buf
- char* buf = graph._C_build_and_expand (brace_expr, s, n, sep);
+ char* buf = graph.build_and_expand (brace_expr, s, n, sep);
if (!buf)
return 0;