Beispiel #1
0
CommonTokenPtr Lexer::emit()
{
    /* We could check pointers to token factories and so on, but
    * we are in code that we want to run as fast as possible
    * so we are not checking any errors. So make sure you have installed an input stream before
    * trying to emit a new token.
    */
    CommonTokenPtr token = std::make_shared<CommonToken>();

    /* Install the supplied information, and some other bits we already know
    * get added automatically, such as the input stream it is associated with
    * (though it can all be overridden of course)
    */
    token->setType(state_->type);
    token->setChannel(state_->channel);
    token->setStartIndex(state_->tokenStartCharIndex);
    token->setStopIndex(charIndex());
    token->setInputStream(charStream());

    if (!state_->text.empty())
    {
        token->setText(state_->text);
    }
    
    state_->type = TokenInvalid;
    state_->channel = TokenDefaultChannel;
    state_->tokenStartCharIndex = charIndex();
    state_->text.clear();

    state_->tokenBuffer.push_back(token);
    return token;
}
Beispiel #2
0
std::vector<GlyphQuad> FontAtlas::process(const std::string &text, float *width, float *height) const {
   ASSERT(generated, "Trying to process text with font atlas that is not generated");

   std::vector<GlyphQuad> quads;
   float x = 0.0f, y = fontSize;

   for (char c : text) {
      int index = charIndex(c, glyphCategory);
      if (index < 0) {
         continue;
      }

      stbtt_aligned_quad q;
      stbtt_GetPackedQuad(packData->charData.data(), textureWidth, textureHeight, index, &x, &y, &q, 0);
      quads.push_back({ q.x0, q.y0, q.s0, q.t0, q.x1, q.y1, q.s1, q.t1 });
   }

   if (width) {
      *width = x;
   }
   if (height) {
      *height = y;
   }

   return quads;
}
Beispiel #3
0
String Lexer::text()
{
    if (!state_->text.empty())
    {
        return state_->text;
    }

    return charStream()->substr(state_->tokenStartCharIndex, charIndex());
}
void ZLTextSelectionModel::setBound(Bound &bound, int x, int y) {
	int x1 = x - myArea.hOffset();
	int y1 = y - myArea.vOffset();

	if (myArea.myTextElementMap.empty()) {
		return;
	}

	ZLTextElementMap::const_iterator it = myArea.myTextElementMap.begin();
	for (; it != myArea.myTextElementMap.end(); ++it) {
		if ((it->YStart > y1) || ((it->YEnd > y1) && (it->XEnd > x1))) {
			break;
		}
	}

	if (it != myArea.myTextElementMap.end()) {
		bound.After.ParagraphIndex = it->ParagraphIndex;
		bound.After.ElementIndex = it->ElementIndex;
		bound.After.Exists = true;
		const bool mainDir =
			it->BidiLevel % 2 == (myArea.isRtl() ? 1 : 0);
		bound.After.CharIndex = mainDir ?
			it->StartCharIndex :
			it->StartCharIndex + it->Length;
		if (ZLTextElementRectangle::RangeChecker(x1, y1)(*it)) {
			bound.Before.ParagraphIndex = bound.After.ParagraphIndex;
			bound.Before.ElementIndex = bound.After.ElementIndex;
			bound.Before.Exists = true;
			if (it->Kind == ZLTextElement::WORD_ELEMENT) {
				bound.After.CharIndex = charIndex(*it, x);
				bound.Before.CharIndex = bound.After.CharIndex;
			}
		} else if (it == myArea.myTextElementMap.begin()) {
			bound.Before.Exists = false;
		} else {
			const ZLTextElementRectangle &previous = *(it - 1);
			bound.Before.ParagraphIndex = previous.ParagraphIndex;
			bound.Before.ElementIndex = previous.ElementIndex;
			bound.Before.CharIndex =
				(previous.BidiLevel % 2 == (myArea.isRtl() ? 1 : 0)) ?
					previous.StartCharIndex + previous.Length :
					previous.StartCharIndex;
			bound.Before.Exists = true;
		}
	} else {
		const ZLTextElementRectangle &back = myArea.myTextElementMap.back();
		bound.Before.ParagraphIndex = back.ParagraphIndex;
		bound.Before.ElementIndex = back.ElementIndex;
		bound.Before.CharIndex = back.StartCharIndex + back.Length;
		bound.Before.Exists = true;
		bound.After.Exists = false;
	}
}
int lengthOfLongestSubstring(string s) {
    // for ASCII char sequence, use this as a hashmap
    vector<int> charIndex(256, -1);
    int longest = 0, m = 0;

    for (int i = 0; i < s.length(); i++) {
        m = max(charIndex[s[i]] + 1, m);    // automatically takes care of -1 case
        charIndex[s[i]] = i;
        longest = max(longest, i - m + 1);
    }

    return longest;
}
void ZLTextSelectionModel::setBound(Bound &bound, int x, int y) {
	if (myView.myTextElementMap.empty()) {
		return;
	}

	ZLTextElementMap::const_iterator it = myView.myTextElementMap.begin();
	for (; it != myView.myTextElementMap.end(); ++it) {
		if ((it->YStart > y) || ((it->YEnd > y) && (it->XEnd > x))) {
			break;
		}
	}

	if (it != myView.myTextElementMap.end()) {
		bound.After.ParagraphIndex = it->ParagraphIndex;
		bound.After.ElementIndex = it->ElementIndex;
		bound.After.Exists = true;
		const bool mainDir =
			it->BidiLevel % 2 == myView.myStyle.baseBidiLevel() % 2;
		bound.After.CharIndex = mainDir ?
			it->StartCharIndex :
			it->StartCharIndex + it->Length;
		if (ZLTextElementArea::RangeChecker(x, y)(*it)) {
			bound.Before.ParagraphIndex = bound.After.ParagraphIndex;
			bound.Before.ElementIndex = bound.After.ElementIndex;
			bound.Before.Exists = true;
			if (it->Kind == ZLTextElement::WORD_ELEMENT) {
				bound.After.CharIndex = charIndex(*it, x);
				bound.Before.CharIndex = bound.After.CharIndex;
			}
		} else if (it == myView.myTextElementMap.begin()) {
			bound.Before.Exists = false;
		} else {
			const ZLTextElementArea &previous = *(it - 1);
			bound.Before.ParagraphIndex = previous.ParagraphIndex;
			bound.Before.ElementIndex = previous.ElementIndex;
			bound.Before.CharIndex = (previous.BidiLevel % 2 == myView.myStyle.baseBidiLevel() % 2) ?
				previous.StartCharIndex + previous.Length :
				previous.StartCharIndex;
			bound.Before.Exists = true;
		}
	} else {
		const ZLTextElementArea &back = myView.myTextElementMap.back();
		bound.Before.ParagraphIndex = back.ParagraphIndex;
		bound.Before.ElementIndex = back.ElementIndex;
		bound.Before.CharIndex = back.StartCharIndex + back.Length;
		bound.Before.Exists = true;
		bound.After.Exists = false;
	}
}
Beispiel #7
0
///
/// \brief
/// Returns the next available token from the current input stream.
/// 
/// \param toksource
/// Points to the implementation of a token source. The lexer is 
/// addressed by the super structure pointer.
/// 
/// \returns
/// The next token in the current input stream or the EOF token
/// if there are no more tokens.
/// 
/// \remarks
/// Write remarks for nextToken here.
/// 
/// \see nextToken
///
CommonTokenPtr Lexer::nextTokenStr() {
    /// Loop until we get a non skipped token or EOF
    ///
    for	(;;)
    {
        /// First return previous buffered tokens, if any
        while (!state_->tokenBuffer.empty()) {
            if (state_->tokenBuffer.front()->type() == TokenInvalid) {
                // A real token could have been generated, but "Computer say's naaaaah" and it
                // it is just something we need to skip altogether.
                state_->tokenBuffer.pop_front();
            } else {
                // Good token, not skipped, not EOF token
                auto t = state_->tokenBuffer.front();
                state_->tokenBuffer.pop_front();
                return std::move(t);
            }
        }
        
        if (input_->LA(1) == CharstreamEof)
        {
            // Reached the end of the current stream, nothing more to do if this is
            // the last in the stack.
            //
            CommonTokenPtr teof = std::make_shared<CommonToken>(TokenEof);
            teof->setInputStream(charStream());
            teof->setStartIndex(charIndex());
            teof->setStopIndex(charIndex());
            return teof;
        }
        
        // Now call the matching rules and see if we can generate a new token

        // Start out without an exception
        state_->error		= false;
        state_->failed		= false;
        
        // Record the start of the token in our input stream.
        state_->channel = TokenDefaultChannel;
        state_->tokenStartCharIndex	= charStream()->index();
        state_->text = ANTLR3_T("");
        
        if (filteringMode_) {
            antlr3::MarkerPtr m = input_->mark();
            state_->backtracking = 1; // No exceptions

            // Call the generated lexer, see if it can get a new token together.
            mTokens();
            state_->backtracking = 0;

            // mTokens backtracks with synpred at state_->backtracking==2
            // and we set the synpredgate to allow actions at level 1.

            if (state_->failed)
            {
                // Advance one char and try again
                m->rewind();
                input_->consume();
                continue;
            }
        } else {
            // Call the generated lexer, see if it can get a new token together.
            mTokens();

            if  (state_->error  == true)
            {
                // Recognition exception, report it and try to recover.
                state_->failed = true;
                reportError();
                recover();
                continue;
            }
        }

        if (state_->tokenBuffer.empty())
        {
            // Emit the real token, which adds it in to the token stream basically
            emit();
        }
    }
}
bool ZLTextSelectionModel::selectWord(int x, int y) {
	clear();

	const ZLTextElementRectangle *rectangle = myArea.elementByCoordinates(x, y);
	if (rectangle == 0) {
		return false;
	}

	int startIndex = 0;
	int endIndex = 1;
	switch (rectangle->Kind) {
		default:
			return false;
		case ZLTextElement::IMAGE_ELEMENT:
			break;
		case ZLTextElement::WORD_ELEMENT:
		{
			ZLTextWordCursor cursor = myArea.startCursor();
			cursor.moveToParagraph(rectangle->ParagraphIndex);
			const ZLTextWord &word = (const ZLTextWord&)cursor.paragraphCursor()[rectangle->ElementIndex];
			ZLUnicodeUtil::Ucs4String ucs4string;
			ZLUnicodeUtil::utf8ToUcs4(ucs4string, word.Data, word.Size);
			startIndex = charIndex(*rectangle, x);
			if (startIndex == word.Length) {
				--startIndex;
			}
			endIndex = startIndex + 1;
			ZLUnicodeUtil::Ucs4Char ch = ucs4string[startIndex];
			if (ZLUnicodeUtil::isLetter(ch) || (('0' <= ch) && (ch <= '9'))) {
				while (--startIndex >= 0) {
					ch = ucs4string[startIndex];
					if (!ZLUnicodeUtil::isLetter(ch) && ((ch < '0') || (ch > '9'))) {
						break;
					}
				}
				++startIndex;
				while (++endIndex <= word.Length) {
					ch = ucs4string[endIndex - 1];
					if (!ZLUnicodeUtil::isLetter(ch) && ((ch < '0') || (ch > '9'))) {
						break;
					}
				}
				--endIndex;
			}
		}
	}

	myFirstBound.Before.Exists = true;
	myFirstBound.Before.ParagraphIndex = rectangle->ParagraphIndex;
	myFirstBound.Before.ElementIndex = rectangle->ElementIndex;
	myFirstBound.Before.CharIndex = startIndex;
	myFirstBound.After = myFirstBound.Before;

	mySecondBound.Before = myFirstBound.Before;
	mySecondBound.Before.CharIndex = endIndex;
	mySecondBound.After = mySecondBound.Before;

	myIsEmpty = false;
	myTextIsUpToDate = false;
	myRangeVectorIsUpToDate = false;

	copySelectionToClipboard(ZLDialogManager::CLIPBOARD_SELECTION);

	return true;
}
Beispiel #9
0
bool parseSocket(_CLIENT* client, sqlite3* db, int announceInterval){
	char buf[1024];
	char* buffer=buf;
	int size,c;
	bool handled=false;
	ANNOUNCE_REQUEST ar;
	socklen_t socklen;
	struct sockaddr_storage conn;
	
	size=recv(client->socket,buf,sizeof(buf)-1,MSG_PEEK);
	if(size==0||size==sizeof(buf)-1){
		ERROR(printf("Erroneous link\n"));
		handled=true;
	}
	else{
		buf[size]=0;
		SPAM(printf("%d bytes of data received\n",size));
		if(!strncmp(buffer,"GET /",5)){
			buffer+=5;
			//http client
			if(!strncmp(buffer,"announce",8)){
				buffer+=8;
				//announce request, parse
				memset(&ar,0,sizeof(ANNOUNCE_REQUEST));
				socklen=sizeof(struct sockaddr_storage);
				if(getpeername(client->socket,(struct sockaddr*)&conn,&socklen)!=-1){
					//check mandatory arguments
					c=charIndex((unsigned char*)buffer,' ');
					if(c!=-1){
						buffer[c]=0;//terminate buffer after request string
						char* hash=textAfter(buffer,"info_hash=");
						char* peerid=textAfter(buffer,"peer_id=");
						char* port=textAfter(buffer,"port=");
						char* event=textAfter(buffer,"event=");
						char* numwant=textAfter(buffer,"numwant=");
						char* left=textAfter(buffer,"left=");
						
						if(hash&&peerid&&port){
							//get compact flag
							ar.compact=false;
							if(strstr(buffer,"compact=1")){
								ar.compact=true;
							}
							
							//get no_peer_id flag
							ar.no_peer_id=false;
							if(strstr(buffer,"no_peer_id=1")){
								ar.no_peer_id=true;
							}
							
							//get event data
							ar.event=NONE;
							if(event&&(!strncmp(event,"st",2)||!strncmp(event,"co",2))){
								switch(event[2]){
									case 'a':
										ar.event=STARTED;
										break;
									case 'o':
										ar.event=STOPPED;
										break;
									case 'm':
										ar.event=COMPLETED;
										break;
								}
							}
							
							//calculate parameter lengths
							int hash_len=httpParamLength(hash);
							int peerid_len=httpParamLength(peerid);
							
							if(decodedStrlen(hash,hash_len)==MAX_HASH_LEN&&decodedStrlen(peerid,peerid_len)==MAX_HASH_LEN){
								strncpy(ar.info_hash,hash,hash_len);//FIXME TODO ensure string termination
								strncpy(ar.peer_id,peerid,peerid_len);
								ar.port=strtoul(port,NULL,10);
								
								if(numwant){
									ar.numwant=strtoul(numwant,NULL,10);
								}
								if(ar.numwant>MAXPEERS_SENT||ar.numwant<=0){//there are clients that actually send 0 here?
									ar.numwant=MAXPEERS_SENT;
								}
								
								ar.left=0;
								if(left){
									ar.left=strtoul(left,NULL,10);
								}
								
								if(conn.ss_family==AF_INET6){
									inet_ntop(conn.ss_family,&(((struct sockaddr_in6*)&conn)->sin6_addr),ar.ip,INET6_ADDRSTRLEN);
									ar.protover=6;
								}
								else{
									inet_ntop(conn.ss_family,&(((struct sockaddr_in*)&conn)->sin_addr),ar.ip,INET6_ADDRSTRLEN);
									ar.protover=4;
								}
								
								//DEBUG(printf("info_hash: %s, peer_id: %s, addr %s, port %d\n",ar.info_hash,ar.peer_id,ar.ip,ar.port));
								
								track(client->socket, db, &ar, announceInterval);
								handled=true;
							}
							else{
								//vital parameters out of bounds
								if(client->recv_iter>=MAX_RECV_ITER){
									sendHttpHeaders(client->socket,"400 Bad Syntax","X-Failure-Reason: Failed Parameter Validation\r\n");
								}
							}
						}
						else{
							//request is missing vital data
							if(client->recv_iter>=MAX_RECV_ITER){
								sendHttpHeaders(client->socket,"408 Timed out","X-Failure-Reason: Missing parameters\r\n");
							}
						}
					}
					else{
						//request was not properly terminated
						if(client->recv_iter>=MAX_RECV_ITER){
							sendHttpHeaders(client->socket,"400 Bad Syntax","X-Failure-Reason: Thats just plain wrong\r\n");
						}
					}
				}
				else{
					ERROR(printf("Call to getpeername failed\n"));
					handled=true;
				}
			}
			else if(!strncmp(buffer, "scrape", 6)){
				buffer+=6;
				//scrape request
				c=charIndex((unsigned char*)buffer, ' ');
				if(c!=-1){
					buffer[c]=0;
					char* hash=textAfter(buffer,"info_hash=");
					if(!hash){
						scrape(client->socket, db, NULL);
						handled=true;
					}
					else{
						//get hash
						int hash_len=httpParamLength(hash);
						if(decodedStrlen(hash,hash_len)==MAX_HASH_LEN){
							unsigned char hashbuf[MAX_URLENC_HASH_LEN+1];
							memcpy(hashbuf,hash,hash_len);
							hashbuf[hash_len]=0;
							destructiveURLDecode(hashbuf);
							hashEncodeHex(hashbuf,(sizeof(hashbuf)/sizeof(unsigned char))-1);
							scrape(client->socket, db, hashbuf);
							handled=true;
						}
					}
				}
				else{
					//request was not properly terminated
					if(client->recv_iter>=MAX_RECV_ITER){
						sendHttpHeaders(client->socket,"400 Bad Syntax","X-Failure-Reason: Thats just plain wrong\r\n");
					}
				}
			}
			else{
				//some other http
				sendHttpHeaders(client->socket,"200 OK",STANDARD_HEADER);
				for(c=0;c<10;c++){
					sendString(client->socket,"Making Milhouse cry is not a science project\r\n");
				}
				handled=true;
			}
		}
		else{
			//non-http client or wrong operation
			sendHttpHeaders(client->socket,"405 Not supported",STANDARD_HEADER);
			handled=true;
		}
	}
	
	if(handled||client->recv_iter>=MAX_RECV_ITER){
		handled=true;
		//flush kernel buffers
		recv(client->socket,buf,sizeof(buf)-1,0);
	}
	return handled;
}
bool ZLTextSelectionModel::selectWord(const ZLTextElementArea & area, int x, int y)
{
    if (ZLTextElementArea::RangeChecker(x, y)(area)) {
        int startIndex = 0;
        int endIndex = 1;
        switch (area.Kind) {
            default:
                return false;
            case ZLTextElement::IMAGE_ELEMENT:
                break;
            case ZLTextElement::WORD_ELEMENT:
                {
                    ZLTextWordCursor cursor = myView.startCursor();
                    cursor.moveToParagraph(area.ParagraphIndex);
                    const ZLTextWord &word = (const ZLTextWord&)cursor.paragraphCursor()[area.ElementIndex];
                    ZLUnicodeUtil::Ucs4String ucs4string;
                    ZLUnicodeUtil::utf8ToUcs4(ucs4string, word.Data, word.Size);
                    startIndex = charIndex(area, x);
                    if (startIndex == word.Length) {
                        --startIndex;
                    }
                    endIndex = startIndex + 1;
                    ZLUnicodeUtil::Ucs4Char ch = ucs4string[startIndex];

                    // TODO: Get rid of non-letter character.
                    while (!ZLUnicodeUtil::isLetter(ch) && ++startIndex < word.Length)
                    {
                        ch = ucs4string[startIndex];
                    }
                    if (startIndex >= word.Length)
                    {
                        break;
                    }

                    if (ZLUnicodeUtil::isLetter(ch) || (('0' <= ch) && (ch <= '9'))) {
                        while (--startIndex >= 0) {
                            ch = ucs4string[startIndex];
                            if (!ZLUnicodeUtil::isLetter(ch) && ((ch < '0') || (ch > '9'))) {
                                break;
                            }
                        }
                        ++startIndex;
                        while (++endIndex <= word.Length) {
                            ch = ucs4string[endIndex - 1];
                            if (!ZLUnicodeUtil::isLetter(ch) && ((ch < '0') || (ch > '9'))) {
                                break;
                            }
                        }
                        --endIndex;
                    }
                }
        }

        myFirstBound.Before.Exists = true;
        myFirstBound.Before.ParagraphIndex = area.ParagraphIndex;
        myFirstBound.Before.ElementIndex = area.ElementIndex;
        myFirstBound.Before.CharIndex = startIndex;
        myFirstBound.After = myFirstBound.Before;

        mySecondBound.Before = myFirstBound.Before;
        mySecondBound.Before.CharIndex = endIndex;
        mySecondBound.After = mySecondBound.Before;

        myIsEmpty = false;
        myTextIsUpToDate = false;
        myRangeVectorIsUpToDate = false;
        myDoUpdate = false;

        myArea = area;

        return true;
    }
    return false;
}