Alien-SmokeQt

 view release on metacpan or  search on metacpan

generator/parser/stringhelpers.cpp  view on Meta::CPAN

static inline bool isWhite( char c ) {
  return QChar(c).isSpace();
}

void rStrip( const QString& str, QString& from ) {
  if( str.isEmpty() ) return;

  int i = 0;
  int ip = from.length();
  int s = from.length();

  for( int a = s-1; a >= 0; a-- ) {
      if( isWhite( from[a] ) ) {
          continue;
      } else {
          if( from[a] == str[i] ) {
              i++;
              ip = a;
              if( i == (int)str.length() ) break;
          } else {
              break;
          }
      }
  }

  if( ip != (int)from.length() ) from = from.left( ip );
}

void strip( const QString& str, QString& from ) {
  if( str.isEmpty() ) return;

  int i = 0;
  int ip = 0;
  int s = from.length();

  for( int a = 0; a < s; a++ ) {
      if( isWhite( from[a] ) ) {
          continue;
      } else {
          if( from[a] == str[i] ) {
              i++;
              ip = a+1;
              if( i == (int)str.length() ) break;
          } else {
              break;
          }
      }
  }

  if( ip ) from = from.mid( ip );
}

void rStrip( const QByteArray& str, QByteArray& from ) {
  if( str.isEmpty() ) return;

  int i = 0;
  int ip = from.length();
  int s = from.length();

  for( int a = s-1; a >= 0; a-- ) {
      if( isWhite( from[a] ) ) { ///@todo Check whether this can cause problems in utf-8, as only one real character is treated!
          continue;
      } else {
          if( from[a] == str[i] ) {
              i++;
              ip = a;
              if( i == (int)str.length() ) break;
          } else {
              break;
          }
      }
  }

  if( ip != (int)from.length() ) from = from.left( ip );
}

void strip( const QByteArray& str, QByteArray& from ) {
  if( str.isEmpty() ) return;

  int i = 0;
  int ip = 0;
  int s = from.length();

  for( int a = 0; a < s; a++ ) {
      if( isWhite( from[a] ) ) { ///@todo Check whether this can cause problems in utf-8, as only one real character is treated!
          continue;
      } else {
          if( from[a] == str[i] ) {
              i++;
              ip = a+1;
              if( i == (int)str.length() ) break;
          } else {
              break;
          }
      }
  }

  if( ip ) from = from.mid( ip );
}
QString formatComment( const QString& comment ) {
  QString ret;

  QStringList lines = comment.split( '\n', QString::KeepEmptyParts );

  if ( !lines.isEmpty() ) {

    QStringList::iterator it = lines.begin();
    QStringList::iterator eit = lines.end();

    // remove common leading chars from the beginning of lines
    for( ; it != eit; ++it ) {
        strip( "///", *it );
        strip( "//", *it );
        strip( "**", *it );
        rStrip( "/**", *it );
    }

    ret = lines.join( "\n" );
  }

  return ret.trimmed();
}

QByteArray formatComment( const QByteArray& comment ) {
  QByteArray ret;

  QList<QByteArray> lines = comment.split( '\n' );

  if ( !lines.isEmpty() ) {

    QList<QByteArray>::iterator it = lines.begin();
    QList<QByteArray>::iterator eit = lines.end();

    // remove common leading chars from the beginning of lines
    for( ; it != eit; ++it ) {
        strip( "///", *it );
        strip( "//", *it );
        strip( "**", *it );
        rStrip( "/**", *it );
    }

    foreach(const QByteArray& line, lines) {
      if(!ret.isEmpty())
        ret += "\n";
      ret += line;



( run in 0.911 second using v1.01-cache-2.11-cpan-13bb782fe5a )