From 85dbf17b7ce704d2ef7e999098e5fd9c97a88eb7 Mon Sep 17 00:00:00 2001 From: "darpan.ka" Date: Tue, 16 Jul 2013 17:30:04 +0530 Subject: [PATCH] [3.0][cherry-pick]Changes in Skip() and GetNextTokenWithoutPAttern() method Change-Id: I6e26cadaaccb33eb041ef8a0cccf7fd3954e9652 Signed-off-by: darpan.ka --- src/base/utility/FBaseUtil_ScannerImpl.cpp | 210 ++++++++--------------------- 1 file changed, 54 insertions(+), 156 deletions(-) diff --git a/src/base/utility/FBaseUtil_ScannerImpl.cpp b/src/base/utility/FBaseUtil_ScannerImpl.cpp index 794d4dd..360b59b 100644 --- a/src/base/utility/FBaseUtil_ScannerImpl.cpp +++ b/src/base/utility/FBaseUtil_ScannerImpl.cpp @@ -610,29 +610,6 @@ void _ScannerImpl::Skip(const String& str) { __position = __position + length; } - matchedStrList.RemoveAll(); - - //Skip if there is any delimiters before the next token - RegularExpression re; - if (__delimiter == DEFAULT_DELIMITER_PATTERN) - { - r = re.Construct(DEFAULT_DELIMITER_PATTERN_ZERO_OR_MORE, REGEX_UNICODE); - } - else - { - r = re.Construct(__delimiter, REGEX_UNICODE); - } - - res = re.Match(String(__pParseStr + __position), false, &matchedStrList); - matches = matchedStrList.GetCount(); - if (matches > 0) - { - String temp = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); - int length = temp.GetLength(); - __position = __position + length; - matchedStrList.RemoveAll(); - } - } void _ScannerImpl::Skip(const RegularExpression& pattern) @@ -688,6 +665,7 @@ result _ScannerImpl::GetNextTokenWithoutPattern(String& ret, int& length) bool res = regDelim.Match(inputStr, false, &matchedStrList); int matches = matchedStrList.GetCount(); + if (matches > 0) { String delimiter1 = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); @@ -695,67 +673,58 @@ result _ScannerImpl::GetNextTokenWithoutPattern(String& ret, int& length) if (!delimiter1.IsEmpty()) // 1st delimiter is not empty { int delimLength1 = delimiter1.GetLength(); - int index1 = 0; - inputStr.IndexOf(delimiter1, 0, index1); + int delim1Index = 0; + inputStr.IndexOf(delimiter1, 0, delim1Index); + if (delim1Index > 0) // Delimiter1 appears after the token i.e followed by token + { + inputStr.SubString(0, delim1Index, ret); // Extract the string in between first and second delimiter + length += delim1Index; + return E_SUCCESS; + } // When the delimiter exists in first position, find the 2nd delimiter - if ((index1 == 0) && (__position == 0)) + length += delimLength1; + String tmpStr(__pParseStr + __position + length); + SysTryReturnResult(NID_BASE_UTIL, tmpStr != null, E_DATA_NOT_FOUND, "There are no more tokens in input string"); + + matchedStrList.RemoveAll(); + res = regDelim.Match(tmpStr, false, &matchedStrList); + matches = matchedStrList.GetCount(); + if (matches > 0) { - length += delimLength1; - String tmpStr(__pParseStr + length); - - matchedStrList.RemoveAll(); - res = regDelim.Match(tmpStr, false, &matchedStrList); - matches = matchedStrList.GetCount(); - if (matches > 0) + String delimiter2 = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); + // If the 2nd delimiter is empty, the token will be one character followed by 1st delimiter. + if (delimiter2.IsEmpty()) + { + ret.Clear(); + ret.Append(*(__pParseStr + __position + length)); + length += 1; + } + else { - String delimiter2 = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); - - // If the 2nd delimiter is empty, the token will be one character followed by 1st delimiter. - if (delimiter2.IsEmpty()) - { - ret.Clear(); - ret.Append(*(__pParseStr + length)); - length += 1; - // Though 2nd delimiter is empty, if 3rd non-empty delimiter exists right after the token, length to be moved should be added by 3rd delimiter. - matchedStrList.RemoveAll(); - String tmpStr1(__pParseStr + length); - res = regDelim.Match(tmpStr1, false, &matchedStrList); - matches = matchedStrList.GetCount(); - SysTryReturnResult(NID_BASE_UTIL, matches > 0, E_SUCCESS, "It's a last token."); - - String delimiter3 = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); - int index3 = 0; - tmpStr1.IndexOf(delimiter3, 0, index3); - if (!delimiter3.IsEmpty() && index3 == 0) - { - length += delimiter3.GetLength(); - } - } - else - { - int delimLength2 = delimiter2.GetLength(); - int index2 = 0; - tmpStr.IndexOf(delimiter2, 0, index2); - tmpStr.SubString(0, index2, ret); // Extract the string in between first and second delimiter - length += index2 + delimLength2; - } + int index2 = 0; + tmpStr.IndexOf(delimiter2, 0, index2); + tmpStr.SubString(0, index2, ret); // Extract the string in between first and second delimiter + length += index2; } } - else + else // There are no second delimiters { - inputStr.SubString(0, index1, ret); - length = index1 + delimLength1; + ret.Clear(); + ret.Append(__pParseStr + __position + length); + length += ret.GetLength(); + SysTryReturnResult(NID_BASE_UTIL, length > 0, E_DATA_NOT_FOUND, "There are no more tokens in input string"); } + } else // 1st delimiter is empty { - // Find 2nd delimiter from "__pParseStr + __position + 1" - String tmpStr(__pParseStr + __position + 1); + // Find 2nd delimiter from "__pParseStr + __position + length + 1" + String tmpStr(__pParseStr + __position + length + 1); if (tmpStr == null) // When InputStr is last token, extract the token. { ret.Clear(); - ret.Append(__pParseStr + __position); + ret.Append(__pParseStr + __position + length); length = ret.GetLength(); } else @@ -763,23 +732,21 @@ result _ScannerImpl::GetNextTokenWithoutPattern(String& ret, int& length) matchedStrList.RemoveAll(); res = regDelim.Match(tmpStr, false, &matchedStrList); matches = matchedStrList.GetCount(); - if (matches > 0) + SysTryReturnResult(NID_BASE_UTIL, matches > 0, E_DATA_NOT_FOUND, "There are no more tokens in input string"); + + String delimiter2 = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); + if (delimiter2.IsEmpty()) // If the 2nd delimiter is also empty, the token will be one character followed by 1st delimiter. { - String delimiter2 = *(dynamic_cast< String* >(matchedStrList.GetAt(0))); - if (delimiter2.IsEmpty()) // If the 2nd delimiter is also empty, the token will be one character followed by 1st delimiter. - { - ret.Clear(); - ret.Append(*(__pParseStr + __position)); - length = 1; - } - else - { - int delimLength2 = delimiter2.GetLength(); - int index2 = 0; - inputStr.IndexOf(delimiter2, 0, index2); - inputStr.SubString(0, index2, ret); - length = index2 + delimLength2; - } + ret.Clear(); + ret.Append(*(__pParseStr + __position)); + length = 1; + } + else + { + int index2 = 0; + inputStr.IndexOf(delimiter2, 0, index2); + inputStr.SubString(0, index2, ret); + length = index2; } } } @@ -789,78 +756,9 @@ result _ScannerImpl::GetNextTokenWithoutPattern(String& ret, int& length) ret.Clear(); ret.Append(__pParseStr + __position); length = ret.GetLength(); - if (length <= 0) - { - return E_DATA_NOT_FOUND; - } + SysTryReturnResult(NID_BASE_UTIL, length > 0, E_DATA_NOT_FOUND, "There are no more tokens in input string"); } return r; } - -result _ScannerImpl::GetNextMatchingString(const String& pattern, String& ret, int& length) -{ - result r = E_SUCCESS; - RegularExpression regex; - ArrayList matchedStrList; - - bool res = false; - int matches = 0; - String nextToken; - String matchToken; - - String strPat(ANY_PATTERN); - strPat.Append(DEFAULT_DELIMITER_PATTERN); - - r = regex.Construct(strPat, REGEX_UNICODE); - SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_OPERATION_FAILED, "Regular expression construction failed"); - - r = matchedStrList.Construct(); - SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_OPERATION_FAILED, "Arraylist construction failed"); - - res = regex.Match(String(__pParseStr + __position), false, &matchedStrList); - - SysTryReturnResult(NID_BASE_UTIL, res == true, E_OPERATION_FAILED, "Match Failed"); - - matches = matchedStrList.GetCount(); - if(matches > 0) - { - nextToken = *(String *)matchedStrList.GetAt(0); - matchedStrList.RemoveAll(); - } - - strPat = pattern; - strPat.Append(DEFAULT_DELIMITER_PATTERN); - r = regex.Construct(strPat, REGEX_UNICODE); - SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_OPERATION_FAILED, "Regular expression construction failed"); - - r = matchedStrList.Construct(); - SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_OPERATION_FAILED, "Arraylist construction failed"); - - res = regex.Match(String(__pParseStr + __position), false, &matchedStrList); - SysTryReturnResult(NID_BASE_UTIL, res == true, E_OPERATION_FAILED, "Match Failed"); - - matches = matchedStrList.GetCount(); - if(matches > 0) - { - matchToken = *(String *)matchedStrList.GetAt(0); - matchedStrList.RemoveAll(); - - if((nextToken == matchToken) == true) - { - length = matchToken.GetLength(); - RegularExpression re; - r = re.Construct(DEFAULT_DELIMITER_PATTERN, REGEX_UNICODE); - re.Replace(matchToken, L"", true); - ret = matchToken; - } - else - { - r = E_OPERATION_FAILED; - } - } - - return r; -} - }}} // Osp::Base::Utility -- 2.7.4