#ifndef __RWRETRAIT_H__
#define __RWRETRAIT_H__

/*
 * Template trait declarations for class RWTRegularExpression<char>
 *
 * $Id: retrait.h,v 7.39 1996/05/07 22:24:35 kyle Exp $
 *
 * (c) Copyright 1989 - 1996 Rogue Wave Software, Inc.
 * ALL RIGHTS RESERVED
 *
 * The software and information contained herein are proprietary to, and
 * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
 * intends to preserve as trade secrets such software and information.
 * This software is furnished pursuant to a written license agreement and
 * may be used, copied, transmitted, and stored only in accordance with
 * the terms of such license and with the inclusion of the above copyright
 * notice.  This software and information or any other copies thereof may
 * not be provided or otherwise made available to any other person.
 *
 * Notwithstanding any other lease or license that may pertain to, or
 * accompany the delivery of, this computer software and information, the
 * rights of the Government regarding its use, reproduction and disclosure
 * are as set forth in Section 52.227-19 of the FARS Computer
 * Software-Restricted Rights clause.
 * 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7013.
 * Contractor/Manufacturer is Rogue Wave Software, Inc.,
 * P.O. Box 2328, Corvallis, Oregon 97339.
 *
 * This computer software and information is distributed with "restricted
 * rights."  Use, duplication or disclosure is subject to restrictions as
 * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
 * Computer Software-Restricted Rights (April 1985)."  If the Clause at
 * 18-52.227-74 "Rights in Data General" is specified in the contract,
 * then the "Alternate III" clause applies.
 *
 ***************************************************************************
 *
 * $Log: retrait.h,v $
 * Revision 7.39  1996/05/07 22:24:35  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.38  1996/03/25 22:55:40  kevinj
 * Bug #2941: Added RWTEXPORT_RETURN and changed RWTExport to RWExport
 * in RWTRegularExpressionTrait<char> specialization for MSVC DLL's.
 *
 * Revision 7.37  1996/03/18 23:30:56  jims
 * Add RWTExport to class RWTRegularExpressionTraits<char> (2871)
 *
 * Revision 7.36  1996/03/14 23:05:24  pearson
 * #2828 - undef MAXLONG and MAXSHORT to suppress redefinition warning in
 *         compilation of rec.cpp
 *
 * Revision 7.35  1996/02/18 04:06:37  kyle
 * Millenium adds ability to define BITSPERBYTE for Macintosh platform.
 *
 * Revision 7.34  1996/02/18 01:45:54  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.33  1996/01/30 18:30:27  jims
 * Rearrange comments
 *
 * Revision 7.32  1995/10/24 01:18:59  jims
 * Move RWTRegularExpressionTraits<char>::addSymbolSet to rec.cpp
 * Explicitly define BITSPERBYTE for Microsoft
 *
 * Revision 7.31  1995/09/11  18:38:34  pearson
 * Commented staticness
 *
 * Revision 7.30  1995/09/11  16:05:04  pearson
 * Scopus #2074 - Moved definition of addSymbolSet outside of the declaration
 * of RWTRegularExpressionTraits<char> to suppress compiler warnings re:
 * expanding functions with "for" inline.
 *
 * Revision 7.29  1995/09/08  19:13:39  pearson
 * Changed RW_NO_TEMPLATE_TYPEDEF_IN_OUT_OF_LINE_ARG_LIST to RW_BROKEN_TEMPLATE_TYPEDEFS
 *
 * Revision 7.28  1995/09/05  19:22:42  jims
 * Use new copyright macro
 *
 * Revision 7.27  1995/08/30  00:19:35  kevinj
 * Added RW_NO_EXCEPTIONS.
 *
 * Revision 7.26  1995/07/03  22:24:43  jims
 * Cast setString indices to (size_t) to avoid ambiguities
 *
 * Revision 7.25  1995/06/27  23:40:49  kevinj
 * #1876: Added RW_NO_STL to guard against futile compilation of classes
 * that depend on the existence of C++ Standard Library containers
 * and iterators.
 *
 * Revision 7.24  1995/04/12  05:50:22  jims
 * cast RWTwoToThePowerOf<BITSPERBYTE>::value to int
 * for use as template parameter
 *
 * Revision 7.23  1995/01/26  17:39:40  kevinj
 * Bug #1038: Infinite loop on some pattern matching.
 *
 * Revision 7.22  1995/01/21  01:20:02  kevinj
 * Completed port to Solaris Cafe 3.0.
 *
 * Revision 7.22  1995/01/21  01:20:02  kevinj
 * Completed port to Solaris Cafe 3.0.
 *
 * Revision 7.21  1995/01/16  21:21:56  kevinj
 * Optimized for speed with RWCRegexp.
 *
 * Revision 7.21  1995/01/16  21:21:56  kevinj
 * Optimized for speed with RWCRegexp.
 *
 * Revision 7.20  1995/01/10  21:12:06  kevinj
 * Ported to Solaris Cafe.
 *
 * Revision 7.19  1995/01/04  20:55:04  kevinj
 * Second code review tasks completed.
 *
 * Revision 7.18  1994/12/30  23:56:44  kevinj
 * Added RW_NO_POSIX_RE.  Made classes MT safe.  Commented and cleaned up code.
 * Combined NFA arrays in regex_t into one array called "node".
 * Removed RWREMatch.
 */

#include "rw/cstring.h"

#if !defined(RW_NO_STL) && !defined(RW_NO_EXCEPTIONS)

#include "rw/tvordvec.h"
#include "rw/tbitvec.h"


#if defined( macintosh )  //jvd,djk
# define BITSPERBYTE RWBITSPERBYTE
#else
# ifndef _MSC_VER
#   ifdef __TURBOC__    //Suppress duplicate definition warning in Borland 4.52
#     undef MAXSHORT
#     undef MAXLONG
#   endif
#   include <values.h>
# else
#   define BITSPERBYTE 8
# endif  /* _MSC_VER */
#endif /* (macintosh) */

/////////////////////////////////////////////////////////////////////////////
//
// RWTwoToThePowerOf
//
//  Calculates powers of two at compile time.
//  Used to initialize RWTRegularExpressionTraits::symbolSetT.
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(RW_NO_EXPRESSION_TEMPLATES)
template<int N>
class RWTwoToThePowerOf
{
public:
  enum {value = 2 * RWTwoToThePowerOf<N - 1>::value};
};

class RWTwoToThePowerOf<0>
{
public:
  enum {value = 1};
};
#endif


/////////////////////////////////////////////////////////////////////////////
//
// RWTRegularExpressionTraits
//
//  Trait classes that enables RWTRegularExpression to specialize on
//  different types.
//
/////////////////////////////////////////////////////////////////////////////

#if defined(RW_BROKEN_TEMPLATE_TYPEDEFS)
  template<class charT> class RWTExport RWTRegularExpressionTraits
    {
    public:
    typedef     int   tStringT;
#   if defined(RW_NO_POSIX_RE)
      typedef   int   symbolSetT;
      typedef   int   symbolSetTVector;
#   endif
    };  // Dummy typedefs.
#else
  template<class charT> class RWTRegularExpressionTraits{};
#endif

#undef RWC
#define RWC(name, value) static char name() {return(value);}

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

class RWExport RWTRegularExpressionTraits<char>
{
public:

  // Used in RWTRegularExpression<charT> to specialize on charT
  typedef RWCString                                     tStringT;

  // Extended regular expression metacharacters.
  RWC(a,              'a');
  RWC(b,              'b');
  RWC(bar,            '|');
  RWC(backslash,      '\\');
  RWC(backspace,      '\b');
  RWC(c,              'c');
  RWC(colon,          ':');
  RWC(closeBrace,     '}');
  RWC(closeBracket,   ']');
  RWC(closeParen,     ')');
  RWC(d,              'd');
  RWC(dash,           '-');
  RWC(dollar,         '$');
  RWC(dot,            '.');
  RWC(e,              'e');
  RWC(eight,          '8');
  RWC(equal,          '=');
  RWC(f,              'f');
  RWC(five,           '5');
  RWC(formfeed,       '\f');
  RWC(four,           '4');
  RWC(hat,            '^');
  RWC(n,              'n');
  RWC(newlineChar,    '\n');
  RWC(nine,           '9');
  RWC(one,            '1');
  RWC(openBrace,      '{');
  RWC(openBracket,    '[');
  RWC(openParen,      '(');
  RWC(plus,           '+');
  RWC(questionMark,   '?');
  RWC(r,              'r');
  RWC(seven,          '7');
  RWC(six,            '6');
  RWC(star,           '*');
  RWC(t,              't');
  RWC(tab,            '\t');
  RWC(three,          '3');
  RWC(two,            '2');
  RWC(x,              'x');
  RWC(zero,           '0');

#if defined(RW_NO_POSIX_RE)
#if defined(RW_NO_EXPRESSION_TEMPLATES)
  typedef RWTBitVec<256>                                    symbolSetT;
#else
  typedef RWTBitVec<(int)RWTwoToThePowerOf<BITSPERBYTE>::value>  symbolSetT;
#endif
  typedef RWTValOrderedVector<symbolSetT>                   symbolSetTVector;
    // Used in RWTRegularExpression<charT> to match charT input symbols
    // in range expressions and character class expressions.

  static RWBoolean  isChar(){return TRUE;}  // Used to see if can use "fast" RE.

  // nullCharT returns a null character value for any symbol type.
  static const char     nullCharT() {return 0;}

  // Adds character class expression match maps, and returns
  //   index of new symbol set entry:
  static size_t   addSymbolSet
  (
    symbolSetTVector&         symbolSetTVector, // Ref to symbol set array.
    const tStringT&           setString     // Character class string in RE.
  );

  // Returns TRUE if "symbol" has a match in symbol set 
  //   pointed to by "setIndex":
  static RWBoolean isMatchedInSymbolSet
  (
    symbolSetT&       symbolSet,  // Symbol set of character class expression.
                                  // Can't be const because
                                  // RWTBitVec::operator[] isn't const.
    const char        symbol
  )
  {
    size_t  i(symbol);

    if (symbolSet.testBit(i))
      return TRUE;

    return FALSE;
  }
#endif  // RW_NO_POSIX_RE
};  // RWTRegularExpressionTraits<char>

#undef RWC

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* !defined(RW_NO_STL) */

#endif /* __RWRETRAIT_H__ */
