/*
 * exregexp.cpp: Example using extended regular expressions
 *
 * $Id: exregexp.cpp,v 7.3 1995/10/24 01:22:45 jims 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: exregexp.cpp,v $
 * Revision 7.3  1995/10/24 01:22:45  jims
 * Small changes to output format
 * Init variable i before use
 *
 * Revision 7.2  1995/09/06  23:43:22  jims
 * Add header section with copyright macro
 *
 */

#include "rw/cstring.h"
#include <iostream.h>
#include "rw/re.h"

/*
 * This example demonstrates how extended regular expressions can be
 * combined to perform complex global search and replace operations.
 *
 * Note:  Since the C++ preprocessor handles special characters with the
 *        "\" escape syntax, extended regular expressions require the  
 *        use of a double back slash whenever you wish to escape a 
 *        special character.
 */

main()
{
  RWCRExpr rgxp(" [A-Za-z\\_]+((ton)|(burg)|([cC]ity))");
  RWCString a("There is no joy in Edmonton. \n");
  RWCString b(a);
  b.replace(rgxp," Healdsburg");
  RWCString c(a);
  c.replace(rgxp, " Sioux_City");
  RWCString result(a+b+c);

  cout << endl << result << endl;
 
  cout << "Replace all town names with 'Mudville'..." << endl << endl;

  result.replace(rgxp, " Mudville", RWCString::all);

  cout << result << endl << endl;
 
  //Here extended regular expressions are used to create a highly
  //simplified syntactic scanner.  
  RWCRExpr int_constant("[\\+\\-]?[0-9]+");
  RWCRExpr float_constant("[\\+\\-]?[0-9]+\\.[0-9]*");
  RWCRExpr identifier("[A-Za-z_][A-Za-z0-9_]*");

  //Create a string with two simple assignment statements in it
  RWCString exp1("var  =  4 + -2.5;");  
  RWCString exp2("var2 = +4 -  0.5;"); 
  RWCString stmts(exp1 + "\n\t" + exp2);

  //Display all identifiers in the program segment
  cout << "In the pair of C++ statements: " << endl
       << '\t' << stmts << endl << endl
       <<  "Identifiers:\t\t";

  size_t i = 0;
  size_t size = 0;

  while( i < stmts.length()  && i != RW_NPOS)
  { 
    i += size;
    i = stmts.index(identifier, &size, i);
    if (size && i != RW_NPOS)
      cout << stmts(i, size) << "  ";
  }
  
  //Typically a scanner will replace a recognized sequence with a token
  //identifying what type of sequence it is.  The focus of this example,
  //however, is to demonstrate how to iterate through all sequences which
  //match a given extended regular expression.  Therefore.....
  stmts.replace(identifier, "", RWCString::all);
  
  // Display all floating point constants in the program segment
  cout << endl << "Float constants:\t";
  i = size = 0; 
  while( i < stmts.length()  && i != RW_NPOS)
  { 
    i += size;
    i = stmts.index(float_constant, &size, i);
    if (size && i != RW_NPOS)
      cout << stmts(i, size) << "  ";
  }
  
  stmts.replace(float_constant, "", RWCString::all);
 
  // Display all integer constants in the program segment
  cout << endl <<  "Int constants:\t\t";
  i = size = 0; 
  while( i < stmts.length()  && i != RW_NPOS)
  { 
    i += size;
    i = stmts.index(int_constant, &size, i);
    if (size && i != RW_NPOS)
      cout << stmts(i, size) << "  ";
  }
  
  cout << endl;
  return 0;
}
