Commits

Anonymous committed 17a83ce

toolsfixes: #115441# Added some string helper functions

  • Participants
  • Parent commits a730fee

Comments (0)

Files changed (2)

File l10ntools/inc/export.hxx

 	return buffer.str();
 }
 
+/** @brief Strip off Whitespaces
+ *
+ * This function strips the leading and trailing whitespaces off a
+ * string supplied as the only argument.
+ *
+ * @param str The string to process
+ *
+ * @returns The string with all whitespaces stripped doff
+ */
+std::string strip( std::string str ) {
+
+	if ( str.empty() ) return str;
+
+	// return str if there are no trailing or leading whitespaces at all
+	if ( isalnum(str.at(0)) ) {
+		if ( isalnum(str.at(str.length()-1)) ) {
+			return str;
+		}
+	}
+
+	// iterate from start over the string and find the first position
+	// with a non-whitespace
+	int leadingPos = 0;
+	std::string::iterator iterLeading = str.begin();
+	while ( iterLeading != str.end() ) {
+		if ( isalnum(*iterLeading) ) {
+			break;
+		}
+
+		iterLeading++;
+		leadingPos++;
+	}
+
+	// iterate from end over the string to the beginning and find the
+	// first position with a non-whitespace
+	int trailingPos = str.length();
+	std::string::iterator iterTrailing = str.end();
+	while ( iterTrailing != str.begin() ) {
+		if ( isalnum(*iterTrailing) ) {
+			break;
+		}
+
+		iterTrailing--;
+		trailingPos--;
+	}
+
+	// return the substring from leadingPos to trailingPos
+	return str.substr( leadingPos, trailingPos+1-leadingPos );
+}
+
+/** @brief Replace a String with another
+ *
+ * This function replaces string A with string B in string str. All
+ * occurances are replaced.
+ *
+ * @param str The string to search
+ * @param A The string that is to be replaced
+ * @param B The string to be inserted
+ *
+ * @returns The result string with all occurances replaced
+ */
+std::string replace( std::string str, std::string A, std::string B ) {
+
+	// return the source string if either the source string was empty or
+	// the string to search for was empty
+	if ( str.empty() || A.empty() ) {
+		return str;
+	}
+
+	int n;
+	std::string tmp = str;
+
+	std::stringstream buffer;
+
+	// iterate over all occurances of A in str
+	while ( (n=tmp.find( A )) != std::string::npos ) {
+		// append the subtring of tmp from zero to the occurance
+		// followed by B
+		buffer << tmp.substr(0,n) << B;
+
+		// set tmp to the remainder that is still needed to be processed
+		tmp = tmp.substr(n+A.length());
+	}
+
+	// append the remainder
+	buffer << tmp;
+
+	return buffer.str();
+}
+
 struct eqstr
 {
 	bool operator()( const char* s1, const char* s2 ) const

File l10ntools/source/export.cxx

 
 		if ( bChild && bChildWithText ) {
 			string sError( "ResId after child definition" );
-			yyerror( sError.c_str());
-			//sError.ReleaseBufferAccess();
+			yyerror( const_cast<char*>(sError.c_str()) );
 			SetError();
 		}
 
 		}
 	}
     if ( sFull.Len() > 255 ) {
-		string sError( "GroupId > 255 chars" );
-	    printf("GroupID = %s\n",sFull.GetBuffer());
-        yyerror( sError.c_str());
+		string sError = "GroupId > 255 chars";
+	    //printf("GroupID = %s\n",sFull.GetBuffer());
+		const char *tempBuffer = sError.c_str();
+        //yyerror(sError.c_str());
 		//sError.ReleaseBufferAccess();
 	}