Git Product home page Git Product logo

Comments (3)

davenger avatar davenger commented on June 22, 2024

I tried to reproduce this and my code works fine:

#include <string>
#include <vector>
#include <stdio.h>
#include <pire/easy.h>

Pire::Scanner scannerFor( std::string regexp ){
    if ( !regexp.size() ) {
        throw 1;
    }

    std::vector<Pire::wchar32> pattern;
    Pire::Scanner s;

    try {
        Pire::Encodings::Utf8().FromLocal( regexp.c_str(), regexp.c_str() + regexp.size(), std::back_inserter(pattern) );

        s = Pire::Lexer( pattern.begin(), pattern.end() )
            .SetEncoding( Pire::Encodings::Utf8() )
            .AddFeature( Pire::Features::CaseInsensitive() )
            .Parse()
            .Surround()
            .Compile<Pire::Scanner>();

    } catch ( ... ) {
        throw 1;
    }

    return s;
}

int main() {
    std::string r;
    r  = "(post|get|put|delete).*http/1\\.(1|0)\r\n.*\r\n\r\n";
    Pire::Scanner sc = scannerFor(r);

    std::string s =
        "GET / HTTP/1.1\r\n"
        "User-Agent: chrome\r\n"
        "Host: ya.ru\r\n"
        "Accept: /\r\n"
        "Proxy-Connection: Keep-Alive\r\n\r\n";

    Pire::Regexp rsc(sc);
    bool a = rsc.Matches(s);
    printf("%s\n", (a ? "match" : "no match"));
    return 0;
}

from pire.

buhtr avatar buhtr commented on June 22, 2024

Test code is:

/* stl */
#include <string>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <iomanip>

/* pire */
#include <pire/easy.h>

namespace pire_test {

class flow {

protected:
    Pire::Scanner scannerFor( std::string regexp, bool useUtf8 ){
        if ( !regexp.size() ) {
            throw 1;
        }

        std::vector<Pire::wchar32> pattern;
        Pire::Scanner s;

        try {
            Pire::Encodings::Utf8().FromLocal( regexp.c_str(), regexp.c_str() + regexp.size(), std::back_inserter(pattern) );

            if ( useUtf8 ) {
                s = Pire::Lexer( pattern.begin(), pattern.end() )
                    .SetEncoding( Pire::Encodings::Utf8() )
                    .AddFeature( Pire::Features::CaseInsensitive() )
                    .Parse()
                    .Surround()
                    .Compile<Pire::Scanner>();
            } else {
                s = Pire::Lexer( pattern.begin(), pattern.end() )
                    .AddFeature( Pire::Features::CaseInsensitive() )
                    .Parse()
                    .Surround()
                    .Compile<Pire::Scanner>();
            }

        } catch ( ... ) {
            throw 1;
        }

        return s;
    }

public:
    void match( std::string s ) {
        Pire::Regexp rsc(_scanner);

        std::cout << std::left 
            << "name: "    << std::setw(10)<< _name 
            << "useUtf8: " << std::setw(4) << ( _useUtf8 ? "YES" : "NO" ) 
            << "match: "   << std::setw(4) << ( rsc.Matches(s) ? "YES" : "NO" )
            << std::endl;
    }

public:
    flow( std::string name, std::string r, bool useUtf8 ) : 
        _name(name), 
        _useUtf8(useUtf8) 
    {
      _scanner = scannerFor( r, _useUtf8 );
    }

    ~flow() {
    }

private:
    Pire::Scanner _scanner;
    std::string _name;
    bool _useUtf8;
};
} // end of namespace 

std::string r("(post|get|put|delete).*http/1\\.(1|0)\r\n.*\r\n\r\n");
pire_test::flow flow1("global1", r, false );
pire_test::flow flow2("global2", r, true );

int main() {
    std::string s =
        "GET / HTTP/1.1\r\n"
        "User-Agent: chrome\r\n"
        "Host: ya.ru\r\n"
        "Accept: /\r\n"
        "Proxy-Connection: Keep-Alive\r\n\r\n";

    pire_test::flow flow3("local1", r, false );
    pire_test::flow flow4("local2", r, true );

    flow1.match(s);
    flow2.match(s);
    flow3.match(s);
    flow4.match(s);

    return 0;
}

Use the pire library as a shared lib all is fine the result is:

name: global1 useUtf8: NO match: YES
name: global2 useUtf8: YES match: YES
name: local1 useUtf8: NO match: YES
name: local2 useUtf8: YES match: YES

Use the pire library as a static lib we have strange encoding problem

name: global1 useUtf8: NO match: YES
name: global2 useUtf8: YES match: NO
name: local1 useUtf8: NO match: YES
name: local2 useUtf8: YES match: YES

I've tried several others regexp to test for "global2 test" and I think the problem is related with ".", i.e
regexp "GET" - match
regexp "." - no match

from pire.

davenger avatar davenger commented on June 22, 2024

The problem was with the order of global vars initialization. This commit fixes the problem.

from pire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.