Skip to content

Instantly share code, notes, and snippets.

@AtnNn
Created March 25, 2014 16:10
Show Gist options
  • Select an option

  • Save AtnNn/9765190 to your computer and use it in GitHub Desktop.

Select an option

Save AtnNn/9765190 to your computer and use it in GitHub Desktop.
#include <string>
#include <re2/re2.h>
#include <iostream>
void test_encoding(std::string enc, bool ignored){
std::cout << "testing " << enc << "\n";
RE2 re2_parser("^\\s*([\\w-]+|\\*)\\s*(?:;\\s*q\\s*=\\s*([0-9]+(?:\\.[0-9]+)?)\\s*)?(?:,|$)");
re2::StringPiece encodings_re2(enc);
std::string name;
std::string qvalue;
while (RE2::FindAndConsume(&encodings_re2, re2_parser, &name, &qvalue)) {
std::cout << "found " << name << ": " << qvalue << "\n";
}
}
int main() {
// These encodings are valid and should result in compression
test_encoding("gzip", true);
test_encoding("gzip", true);
test_encoding("gzip ;q=001", true);
test_encoding("gzip;q =0.1", true);
test_encoding("gzip ;q= 1000.11111 ", true);
test_encoding(" identity;q=1000.1111 , gzip;q=1000.11111", true);
test_encoding("compress, gzip", true);
test_encoding("*", true);
test_encoding("compress;q=0.5, gzip;q=1.0", true);
test_encoding("gzip;q=1.0, identity; q=0.5, *;q=0", true);
test_encoding("geewhiz;q=1.0,geezip;q=2.0,*;q=0.5", true);
test_encoding("geewhiz;q=0.0,geezip;q=2.2,*;q=3", true);
// These encodings are valid but should not result in compression
test_encoding("", false);
test_encoding("*;q=0.0", false);
test_encoding("gzip;q=0.1,identity;q=0.2", false);
test_encoding("gzip;q=1,*;q=1.84", false);
test_encoding("identity;q=1,*;q=0.5", false);
test_encoding("geewhiz", false);
test_encoding("geewhiz;q=1.0", false);
test_encoding("geewhiz;q=1.0,geezip;q=2.0", false);
// These encodings have bad syntax and should fail
test_encoding("gzip:q=0.1", false);
test_encoding("gzip;q=0 .1", false);
test_encoding("gzip;q=0. 1", false);
test_encoding("gzip:q=0x1", false);
test_encoding("gzip;q=", false);
test_encoding("gzip;q", false);
test_encoding("gzip;=0.5", false);
test_encoding("gzip=9.9", false);
test_encoding("gzip;", false);
test_encoding("gzip;s=0.6", false);
test_encoding("gzip;deflate", false);
test_encoding("gzip,deflate,*=0.0", false);
test_encoding("g^zip", false);
test_encoding("g_zip", false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment