Пример #1
0
int match (unsigned char *name, unsigned char *pat)
{
	int matched = 1, len;

	if (*pat == '!') {
		matched = 0;
		++pat;
		if (! *pat)
			return (0);
	}
	while ((len = strlen (pat)) > 0 && pat[len-1] == '*')
		pat[len-1] = 0;
	if (*pat == '^')
		return (submatch (name, ++pat) ? matched : !matched);
	while (*pat == '*')
		++pat;
	if (! *pat)
		return (1);
	if (! pat[1])
		switch (*pat) {
		case 0:
		case '*': return (1);
		case '?': return (*name != 0);
		case '$': return (! *name);
		case '[': return (0);
		default:  return (strchr (name, *pat) != 0);
		}
	for (; *name; ++name)
		if (submatch (name, pat))
			return (matched);
	return (! matched);
}
Пример #2
0
/*
Normally a search for one byte is matched, then two, then three, all the way up
  to the size of the LookAheadBuffer. So I decided to skip the incremental search
  and search for the entire LookAheadBuffer and if the function doesn't find the bytes are 
  equal the function return the next best match(which means if the function look for 18 bytes and they are not found, return
  the number of bytes that did match before it failed to match. The submatch is function returns the number of bytes that
  were equal, which can result up to the bytes total length if both byte strings are equal.
  
  
  ...[][][][][][][][][][][][]|[][][][][][][][][][][][][][]
                             |
     Search Window       Current Pos   LookAheadBuffer
    Up to 4096 bytes                   Up to 18 bytes
    			Sliding Window
    			Up to 4114 bytes
*/
length_offset lzBase::window_search(uint8_t* beginSearchPtr, uint8_t* searchPosPtr,uint8_t* endLABufferPtr, uint8_t* startLBPtr){
	int size=endLABufferPtr-beginSearchPtr;//Size of the entire sliding window
	int n=endLABufferPtr-searchPosPtr;
	length_offset result={0,0};
	int temp=0;
	if(n>size)//If the string that is being looked for is bigger than the string that is being searched
		return result;

	/*This makes sure that search for the searchPosPtr can be searched if an invalid position is given
	  An invalid position occurs if the amount of characters to search in_beginSearchPtr is less than the size
	  of searchPosPtr. In other words there has to be at least n characters left in the string 
	  to have a chance to find n characters*/
	
	do{	
		temp=submatch(startLBPtr,searchPosPtr,n);
		if(result.length<temp){
			result.length=temp;
			result.offset=(int)(searchPosPtr-startLBPtr);
		}
		if(result.length==n)
			return result;
		
		//ReadAheadBuffer is the maximum size of a character match
		
						
	
	}while(startLBPtr-->beginSearchPtr);
	return result;
}
Пример #3
0
Uri::Uri(StringPiece str) : port_(0) {
  static const boost::regex uriRegex(
      "([a-zA-Z][a-zA-Z0-9+.-]*):"  // scheme:
      "([^?#]*)"                    // authority and path
      "(?:\\?([^#]*))?"             // ?query
      "(?:#(.*))?");                // #fragment
  static const boost::regex authorityAndPathRegex("//([^/]*)(/.*)?");

  boost::cmatch match;
  if (UNLIKELY(!boost::regex_match(str.begin(), str.end(), match, uriRegex))) {
    throw std::invalid_argument(to<std::string>("invalid URI ", str));
  }

  scheme_ = submatch(match, 1);
  toLower(scheme_);

  StringPiece authorityAndPath(match[2].first, match[2].second);
  boost::cmatch authorityAndPathMatch;
  if (!boost::regex_match(authorityAndPath.begin(),
                          authorityAndPath.end(),
                          authorityAndPathMatch,
                          authorityAndPathRegex)) {
    // Does not start with //, doesn't have authority
    path_ = authorityAndPath.fbstr();
  } else {
    static const boost::regex authorityRegex(
        "(?:([^@:]*)(?::([^@]*))?@)?"  // username, password
        "(\\[[^\\]]*\\]|[^\\[:]*)"     // host (IP-literal (e.g. '['+IPv6+']',
                                       // dotted-IPv4, or named host)
        "(?::(\\d*))?");               // port

    auto authority = authorityAndPathMatch[1];
    boost::cmatch authorityMatch;
    if (!boost::regex_match(authority.first,
                            authority.second,
                            authorityMatch,
                            authorityRegex)) {
      throw std::invalid_argument(
          to<std::string>("invalid URI authority ",
                          StringPiece(authority.first, authority.second)));
    }

    StringPiece port(authorityMatch[4].first, authorityMatch[4].second);
    if (!port.empty()) {
      port_ = to<uint16_t>(port);
    }

    username_ = submatch(authorityMatch, 1);
    password_ = submatch(authorityMatch, 2);
    host_ = submatch(authorityMatch, 3);
    path_ = submatch(authorityAndPathMatch, 2);
  }

  query_ = submatch(match, 3);
  fragment_ = submatch(match, 4);
}
Пример #4
0
int gcParser::parseBlock(std::string block, std::vector<cmd> &cmds){
    
    static const boost::regex re_fullCmd("("                                    //for parsing the full gcode command
        "([GM]([0-9]{1,3})([ABCDEFHIJKLPQRUVWXYZ](\\+|\\-|\\.|[0-9])+)*)|"
        "([ABCDEFHIJKLPQRSUVWXYZFT]((\\+|\\-|\\.|[0-9])+)*)+"
        ")"),
        re_cmdNumber("[0-9]{1,3}"),                                             //for parsing the command number
        re_params("[ABCDEFHIJKLPQRSTUVWXYZF](\\+|\\-|\\.|[0-9])+");               //for parsing command parameters
    
    int i = strcspn (block.c_str(),";");                                   //remove comments
    block = block.substr(0,i);
    
    block.erase(std::remove_if(block.begin(),block.end(), ::isspace),block.end()); //remove whitespace
    
    boost::sregex_token_iterator iter(block.begin(), block.end(), re_fullCmd, 0);
    boost::sregex_token_iterator end;
    
    for( ; iter != end; ++iter ) {
        cmd a;
        a.letter = NO_LETTER;
        a.number = strtof("", NULL);
        
        std::string s = *iter;
        char letter = s.at(0); //get the command letter
        
        bool is_param = std::find(std::begin(paramLetters), std::end(paramLetters), letter) != std::end(paramLetters);
        
        if(!is_param){ //If this is a new command get the letter and number
            if(letter == 'G' || letter == 'M'){
                a.letter = letter;
                boost::smatch result;
                if (boost::regex_search(s, result, re_cmdNumber)) {
                    std::string submatch(result[0]);
                    a.number = strtof(submatch.c_str(), NULL);
                }
            }
        }
        boost::sregex_token_iterator paramIter(s.begin(), s.end(), re_params, 0);
        boost::sregex_token_iterator paramIterEnd;
        for( ; paramIter != paramIterEnd; ++paramIter ) {
            std::string p = *paramIter;
            char p_letter = p.at(0);
            a.params[p_letter] = strtof(p.substr(1, std::string::npos).c_str(), NULL);
        }
        a.priority = order[a.letter][a.number];
        cmds.push_back(a);
    }
    
    std::sort(cmds.begin(), cmds.end()); //sort according to priority
    
    return 0;
}
Пример #5
0
static Word*
subsub(Word *v, char *s, char *end)
{
	int nmid;
	Word *head, *tail, *w, *h;
	Word *a, *b, *c, *d;
	Bufblock *buf;
	char *cp, *enda;

	a = extractpat(s, &cp, "=%&", end);
	b = c = d = 0;
	if(PERCENT(*cp))
		b = extractpat(cp+1, &cp, "=", end);
	if(*cp == '=')
		c = extractpat(cp+1, &cp, "&%", end);
	if(PERCENT(*cp))
		d = stow(cp+1);
	else if(*cp)
		d = stow(cp);

	head = tail = 0;
	buf = newbuf();
	for(; v; v = v->next){
		h = w = 0;
		if(submatch(v->s, a, b, &nmid, &enda)){
			/* enda points to end of A match in source;
			 * nmid = number of chars between end of A and start of B
			 */
			if(c){
				h = w = wdup(c);
				while(w->next)
					w = w->next;
			}
			if(PERCENT(*cp) && nmid > 0){	
				if(w){
					bufcpy(buf, w->s, strlen(w->s));
					bufcpy(buf, enda, nmid);
					insert(buf, 0);
					free(w->s);
					w->s = strdup(buf->start);
				} else {
					bufcpy(buf, enda, nmid);
					insert(buf, 0);
					h = w = newword(buf->start);
				}
				buf->current = buf->start;
			}
			if(d && *d->s){
				if(w){

					bufcpy(buf, w->s, strlen(w->s));
					bufcpy(buf, d->s, strlen(d->s));
					insert(buf, 0);
					free(w->s);
					w->s = strdup(buf->start);
					w->next = wdup(d->next);
					while(w->next)
						w = w->next;
					buf->current = buf->start;
				} else
					h = w = wdup(d);
			}
		}
		if(w == 0)
			h = w = newword(v->s);
	
		if(head == 0)
			head = h;
		else
			tail->next = h;
		tail = w;
	}
	freebuf(buf);
	delword(a);
	delword(b);
	delword(c);
	delword(d);
	return head;
}
Пример #6
0
char *
replace(char *src, struct strb *tags, struct mail *m, struct rmlist *rml)
{
	const char	*tptr, *alias;
	char		*ptr, *tend, *dst, ch;
	size_t		 i, off, len, tlen;
	int		 strip;

	if (src == NULL)
		return (NULL);
	if (*src == '\0')
		return (xstrdup(""));

	off = 0;
	len = REPLBUFSIZE;
	dst = xmalloc(len);

	strip = 1;
	for (ptr = src; *ptr != '\0'; ptr++) {
		switch (*ptr) {
		case '%':
			break;
		default:
			ENSURE_FOR(dst, len, off, 1);
			dst[off++] = *ptr;
			continue;
		}

		switch (ch = *++ptr) {
		case '\0':
			goto out;
		case '%':
			ENSURE_FOR(dst, len, off, 1);
			dst[off++] = '%';
			continue;
		case '[':
			if ((tend = strchr(ptr, ']')) == NULL) {
				ENSURE_FOR(dst, len, off, 2);
				dst[off++] = '%';
				dst[off++] = '[';
				continue;
			}
			ptr++;

			if (*ptr == ':') {
				strip = 0;
				ptr++;
			}
			if (ptr == tend)
				continue;

			*tend = '\0';
			if ((tptr = find_tag(tags, ptr)) == NULL) {
				*tend = ']';
				ptr = tend;
				continue;
			}
			tlen = strlen(tptr);
			*tend = ']';

			ptr = tend;
			break;
		case ':':
			ch = *++ptr;
			if (ch >= '0' && ch <= '9') {
				tptr = submatch(ch, m, rml, &tlen);
				if (tptr == NULL)
					continue;
				strip = 0;
				break;
			}

			ENSURE_FOR(dst, len, off, 1);
			dst[off++] = ch;
			continue;
		default:
			if (ch >= '0' && ch <= '9') {
				tptr = submatch(ch, m, rml, &tlen);
				if (tptr == NULL)
					continue;
				break;
			}

			alias = NULL;
			if (ALIAS_IDX((u_char) ch) != -1)
				alias = aliases[ALIAS_IDX((u_char) ch)];
			if (alias == NULL)
				continue;

			if ((tptr = find_tag(tags, alias)) == NULL)
				continue;
			tlen = strlen(tptr);
			break;
		}

		if (tlen == 0)
			continue;
		ENSURE_FOR(dst, len, off, tlen);
		if (!strip) {
			memcpy(dst + off, tptr, tlen);
			off += tlen;
			continue;
		}
		for (i = 0; i < tlen; i++) {
			if (strchr(conf.strip_chars, tptr[i]) == NULL)
				dst[off++] = tptr[i];
		}
	}

out:
	ENSURE_FOR(dst, len, off, 1);
	dst[off] = '\0';

	return (dst);
}
Пример #7
0
 const std::string & operator[](int number)
 {
     return submatch(number);
 }