C++ 示例: ContinuousIntegerParser

ContinuousIntegerParser 示例是 BasicIntegerParser 的变体。这两个示例都从输入字符串中解析整数。 ContinuousIntegerParser 使用 连续加载 读取数据。

加载和使用示例

按如下所示加载 ContinuousIntegerParser 示例。

=> CREATE LIBRARY ContinuousIntegerParserLib AS '/home/dbadmin/CIP.so';

=> CREATE PARSER ContinuousIntegerParser AS
LANGUAGE 'C++' NAME 'ContinuousIntegerParserFactory'
LIBRARY ContinuousIntegerParserLib;

以使用 BasicIntegerParser 的同样方式使用它。请参阅C++ 示例: BasicIntegerParser

实施

ContinuousIntegerParserContinuousUDParser 的子类。ContinuousUDParser 的子类将处理逻辑放在 run() 方法中。

    virtual void run() {

        // This parser assumes a single-column input, and
        // a stream of ASCII integers split by non-numeric characters.
        size_t pos = 0;
        size_t reserved = cr.reserve(pos+1);
        while (!cr.isEof() || reserved == pos + 1) {
            while (reserved == pos + 1 && isdigit(*ptr(pos))) {
                pos++;
                reserved = cr.reserve(pos + 1);
            }

            std::string st(ptr(), pos);
            writer->setInt(0, strToInt(st));
            writer->next();

            while (reserved == pos + 1 && !isdigit(*ptr(pos))) {
                pos++;
                reserved = cr.reserve(pos + 1);
            }
            cr.seek(pos);
            pos = 0;
            reserved = cr.reserve(pos + 1);
        }
    }
};

有关 ContinuousUDParser 的更复杂示例,请参阅示例中的 ExampleDelimitedParser。(请参阅下载并运行 UDx 示例代码。) ExampleDelimitedParser 使用块分割器;请参阅 C++ 示例:分隔解析器和块分割器