void test_stringRemoveWordContaining_remove_abc_static(void) {
    int toCompare;
    Text *text = t"abcdefghi";
    String *str = stringNew(text);
    String *test = stringRemoveWordContaining(str,"aaaaaaacb");

    TEST_ASSERT_EQUAL(3,str->start);
    TEST_ASSERT_EQUAL(6,str->length);
    TEST_ASSERT_EQUAL(0,test->start);
    TEST_ASSERT_EQUAL(3,test->length);
    TEST_ASSERT_EQUAL(0x80000000,text->reference);
}
void test_stringRemoveWordContaining_remove_nothing(void) {
    int toCompare;
    Text *text = textNew("1234abcdefghi");
    String *str = stringNew(text);
    String *test = stringRemoveWordContaining(str,"ab");

    TEST_ASSERT_EQUAL(0,str->start);
    TEST_ASSERT_EQUAL(13,str->length);
    TEST_ASSERT_EQUAL(0,test->start);
    TEST_ASSERT_EQUAL(0,test->length);
    TEST_ASSERT_EQUAL(3,text->reference);
}
Пример #3
0
/**
 * Return the one token from the String. The String is updated.
 * If the string is empty, NULL is return.
 *
 * Input:
 *   str  is the String object
 *
 * Possible returned token:
 *    Number, Operator, and Identifier tokens
 */
Token *getToken(String *str) {
	//1 trim left
	//2 check first character
	//	a	-if a number then extract the number string
	//		-convert into integer
	//		-create number token
	//	b	-if alphabetSet then extract the identifier
	//		-create identifier token
	//	c	-if operator set then extract operator symbol
	//		-create operator token

	char charReturn[3];
	Token *tokenReturn = NULL;
	String *strReturn = NULL;
	Number *number = NULL;
	Identifier *identifier = NULL;
	Operator *operator = NULL;

	stringTrimLeft(str);
	if(stringLength(str) == 0)
		return NULL;


	//Number
	if(stringIsCharAtInSet(str,0,numberSet)){
		strReturn = stringRemoveWordContaining(str,numberSet);
		if(stringIsCharAtInSet(str,0,alphabetSet)){
			Throw(ERR_NUMBER_NOT_WELL_FORMED);
		}
		else{
			number = numberNew(stringToInteger(strReturn));
			tokenReturn = (Token *)number;
			stringDel(strReturn);
		}
	}

	//Identifier
	else if(stringIsCharAtInSet(str,0,alphabetSet)){
		strReturn = stringRemoveWordContaining(str,alphaNumericSet);
		identifier = identifierNew(stringSubstringInText(strReturn,0,strReturn->length));
		tokenReturn = (Token *)identifier;
		stringDel(strReturn);
	}

	//Operator
	else if(stringIsCharAtInSet(str,0,operatorSet)){
		charReturn[0] = (char )stringRemoveChar(str);
		charReturn[1] = 0;

		if(stringCharAt(str,0) == charReturn[0] && stringLength(str) != 0){
			if(charReturn[0] == '&'){
				charReturn[0] = '&';
				charReturn[1] = '&';
				charReturn[2] = 0;
				str->start++;
				str->length--;
			}
			else if(charReturn[0] == '|'){
				charReturn[0] = '|';
				charReturn[1] = '|';
				charReturn[2] = 0;
				str->start++;
				str->length--;
			}
		}

		operator = operatorNewBySymbol(charReturn);
		tokenReturn = (Token *)operator;

	}
	else
		Throw(ERR_ILLEGAL_ARGUMENT);


	return tokenReturn;
}
Пример #4
0
/* To extract out number token , identifier token or operator token from the expression string
 * input :
 *			expression is the string that we wanted to extract the token
 * output :
 *			return number token if number is found in string
 *			return idendifier token if identifier is found in string
 * 			return operator token if operator is found in string
 */
Token *getToken(String *expression)
{	
	Number *num; Identifier *iden; Operator *op; File *file;
	int tempStart = 0 , tempLength = 0, hex = 0;  char *tempIden; //temporary store idendifier name
	stringTrim(expression);	//Remove all the spaces in string
  
	/*Character at first position*/
	int charAtThisPos = expression->startindex;
  
  if(expression->length == 0 || expression->length < 0)
    Throw(ERR_EMPTY_STRING);

	//if character start with numbers it is number token
	else if(stringCharAtInSet(expression , charAtThisPos , numSet))
	{	
		String *removedWord = stringRemoveWordContaining (expression , hexNumSet);	//Remove numbers in string
		tempStart = removedWord->startindex;
		tempLength = removedWord->length;
		char *numSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed numbers become substring
		charAtThisPos = expression->startindex;
    
    if(numSubString[0] == '0' && (numSubString[1] == 'x' || numSubString[1] == 'X'))  {
      sscanf(numSubString, "%x", &hex);
      num = numberNew(hex); //get integer from subStringToInteger and create a new Number Token
      num->line = stringSubString(expression , tempStart , tempLength);
    }
    else if(stringCharAtInSet(expression , charAtThisPos , alphaSet))  {
			Throw(ERR_NOT_NUMBER_TOKEN);
    }
    else {
      int integer = subStringToInteger(numSubString); //Convert substring to integer
      num = numberNew(integer); //get integer from subStringToInteger and create a new Number Token
      num->line = stringSubString(expression , tempStart , tempLength);
    }

		return (Token*)num;
	}
	
  else if(stringCharAtInSet(expression , charAtThisPos, alphaNumericSet) && stringCharAtInSet(expression , charAtThisPos + 1, ":")) 
	{
		String *removedWord = stringRemoveWordContaining (expression , folderNameSet); //Remove identifier from string
		tempStart = removedWord->startindex;
		tempLength = removedWord->length;
		char *fileSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed identifier become substring
		file = fileNew(fileSubString); //create a new identifier token
		file->line = stringSubString(expression , tempStart , tempLength);
		
		return (Token*)file;
	}
  
	//if character start with A~Z/a~z or '_' it is identifier token
	else if(stringCharAtInSet(expression , charAtThisPos , alphaNumericSet)) 
	{
		String *removedWord = stringRemoveWordContaining (expression , alphaNumericSet); //Remove identifier from string
		tempStart = removedWord->startindex;
		tempLength = removedWord->length;
		char *idenSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed identifier become substring
		iden = identifierNew(idenSubString); //create a new identifier token
		iden->line = stringSubString(expression , tempStart , tempLength);
		
		return (Token*)iden;
	}
	
	// if character not start with A~Z/a~z , '_' or numbers it is operator token
	else if(stringCharAtInSet(expression , charAtThisPos , opSet)) 
	{
		String *removedWord = stringRemoveOperator(expression , opSet); //Remove operator in string
		tempStart = removedWord->startindex;
		tempLength = removedWord->length;
		char *opSubString = stringSubStringInChars(removedWord , removedWord->length); //Removed operator become substring	
		op = operatorNewByName(opSubString);
		op->line = stringSubString(expression , tempStart , tempLength);
		
		return (Token*)op;
	}
}