Читаем Thinking In C++. Volume 2: Practical Programming полностью

The first test checks for an empty string; in that case, no tests are made, and a copy is returned. Notice that once the end points are found, the string constructor builds a new string from the old one, giving the starting count and the length.

Testing such a general-purpose tool needs to be thorough:.

//: C03:TrimTest.cpp

//{L} ../TestSuite/Test

#include

#include "Trim.h"

#include "../TestSuite/Test.h"

using namespace std;

string s[] = {

  " \t abcdefghijklmnop \t ",

  "abcdefghijklmnop \t ",

  " \t abcdefghijklmnop",

  "a", "ab", "abc", "a b c",

  " \t a b c \t ", " \t a \t b \t c \t ",

  "\t \n \r \v \f",

  "" // Must also test the empty string

};

class TrimTest : public TestSuite::Test {

public:

  void testTrim() {

    test_(trim(s[0]) == "abcdefghijklmnop");

    test_(trim(s[1]) == "abcdefghijklmnop");

    test_(trim(s[2]) == "abcdefghijklmnop");

    test_(trim(s[3]) == "a");

    test_(trim(s[4]) == "ab");

    test_(trim(s[5]) == "abc");

    test_(trim(s[6]) == "a b c");

    test_(trim(s[7]) == "a b c");

    test_(trim(s[8]) == "a \t b \t c");

    test_(trim(s[9]) == "");

    test_(trim(s[10]) == "");

  }

  void run() {

    testTrim();

  }

};

int main() {

  TrimTest t;

  t.run();

  return t.report();

} ///:~

In the array of strings, you can see that the character arrays are automatically converted to string objects. This array provides cases to check the removal of spaces and tabs from both ends, as well as ensuring that spaces and tabs are not removed from the middle of a string.

<p>Removing characters from strings</p>

Removing characters is easy and efficient with the erase( ) member function, which takes two arguments: where to start removing characters (which defaults to 0), and how many to remove (which defaults to string::npos). If you specify more characters than remain in the string, the remaining characters are all erased anyway (so calling erase( ) without any arguments removes all characters from a string). Sometimes it’s useful to take an HTML file and strip its tags and special characters so that you have something approximating the text that would be displayed in the Web browser, only as a plain text file. The following uses erase( ) to do the job:.

//: C03:HTMLStripper.cpp

//{L} ReplaceAll

// Filter to remove html tags and markers

#include

#include

#include

#include

#include

#include

#include "../require.h"

using namespace std;

string& replaceAll(string& context, const string& from,

  const string& to);

string& stripHTMLTags(string& s) {

  static bool inTag = false;

  bool done = false;

  while (!done) {

    if (inTag) {

      // The previous line started an HTML tag

      // but didn't finish. Must search for '>'.

      size_t rightPos = s.find('>');

      if (rightPos != string::npos) {

        inTag = false;

        s.erase(0, rightPos + 1);

      }

      else {

        done = true;

        s.erase();

      }

    }

    else {

      // Look for start of tag:

      size_t leftPos = s.find('<');

      if (leftPos != string::npos) {

        // See if tag close is in this line

        size_t rightPos = s.find('>');

        if (rightPos == string::npos) {

          inTag = done = true;

          s.erase(leftPos);

        }

        else

          s.erase(leftPos, rightPos - leftPos + 1);

      }

      else

        done = true;

    }

  }

  // Remove all special HTML characters

  replaceAll(s, "<", "<");

  replaceAll(s, ">", ">");

  replaceAll(s, "&", "&");

  replaceAll(s, " ", " ");

  // Etc...

  return s;

}

int main(int argc, char* argv[]) {

  requireArgs(argc, 1,

    "usage: HTMLStripper InputFile");

  ifstream in(argv[1]);

  assure(in, argv[1]);

  string s;

  while(getline(in, s))

    if (!stripHTMLTags(s).empty())

      cout << s << endl;

} ///:~

This example will even strip HTML tags that span multiple lines.[32] This is accomplished with the static flag, inTag, which is true whenever the start of a tag is found, but the accompanying tag end is not found in the same line. All forms of erase( ) appear in the stripHTMLFlags( ) function.[33] The version of getline( ) we use here is a global function declared in the header and is handy because it stores an arbitrarily long line in its string argument. You don’t have to worry about the dimension of a character array as you do with istream::getline( ). Notice that this program uses the replaceAll( ) function from earlier in this chapter. In the next chapter, we’ll use string streams to create a more elegant solution.

<p>Comparing strings</p>
Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных