Example #1
0
void FeatureType::GetPreferredNames(string & defaultName, string & intName) const
{
  ParseCommon();

  BestMatchedLangNames matcher;
  ForEachName(matcher);

  defaultName.swap(matcher.m_defaultName);

  if (!matcher.m_nativeName.empty())
    intName.swap(matcher.m_nativeName);
  else if (!matcher.m_intName.empty())
    intName.swap(matcher.m_intName);
  else
    intName.swap(matcher.m_englishName);

  if (defaultName.empty())
    defaultName.swap(intName);
  else
  {
    // filter out similar intName
    if (!intName.empty() && defaultName.find(intName) != string::npos)
      intName.clear();
  }
}
Example #2
0
	void swap(FileListItem& rhs) noexcept
	{
		using std::swap;
		strName.swap(rhs.strName);
		swap(Selected, rhs.Selected);
		swap(PrevSelected, rhs.PrevSelected);
		swap(ShowFolderSize, rhs.ShowFolderSize);
		swap(ShortNamePresent, rhs.ShortNamePresent);
		swap(Colors, rhs.Colors);
		swap(NumberOfLinks, rhs.NumberOfLinks);
		swap(NumberOfStreams, rhs.NumberOfStreams);
		swap(UserFlags, rhs.UserFlags);
		swap(UserData, rhs.UserData);
		swap(Callback, rhs.Callback);
		swap(Position, rhs.Position);
		swap(SortGroup, rhs.SortGroup);
		swap(DizText, rhs.DizText);
		swap(DeleteDiz, rhs.DeleteDiz);
		strOwner.swap(rhs.strOwner);
		swap(CustomColumnData, rhs.CustomColumnData);
		swap(CustomColumnNumber, rhs.CustomColumnNumber);
		swap(CRC32, rhs.CRC32);
		swap(FileAttr, rhs.FileAttr);
		swap(CreationTime, rhs.CreationTime);
		swap(AccessTime, rhs.AccessTime);
		swap(WriteTime, rhs.WriteTime);
		swap(ChangeTime, rhs.ChangeTime);
		swap(FileSize, rhs.FileSize);
		swap(AllocationSize, rhs.AllocationSize);
		swap(StreamsSize, rhs.StreamsSize);
		strShortName.swap(rhs.strShortName);
		swap(ReparseTag, rhs.ReparseTag);
		ContentData.swap(rhs.ContentData);
	}
string BigInteger::multiply(string n1, string n2) {
    if(n1.length() > n2.length())
        n1.swap(n2);

    string res = "0";
    for(int i=n1.length()-1; i>=0; --i) {
        string temp = n2;
        int currentDigit = n1[i]-'0';
        int carry = 0;

        for(int j=temp.length()-1; j>=0; --j) {
            temp[j] = ((temp[j]-'0') * currentDigit) + carry;

            if(temp[j] > 9) {
                carry = (temp[j]/10);
                temp[j] -= (carry*10);
            } else
                carry = 0;

            temp[j] += '0'; // back to string mood
        }

        if(carry > 0)
            temp.insert(0, 1, (carry+'0'));

        temp.append((n1.length()-i-1), '0'); // as like mult by 10, 100, 1000, 10000 and so on

        res = add(res, temp); // O(n)
    }

    while(res[0] == '0' && res.length()!=1) // erase leading zeros
        res.erase(0,1);

    return res;
}
Example #4
0
// 两个非负整数的减法。
string subtract(string number1, string number2)
{
    // 比较被减数和减数的大小,如果被减数小于减数,调整两个数使得相减的操作便于实现。
    // 如果减数大于被减数则交换两个数,置计算结果为负数。
    int sign = 1;
    if (!greaterOrEqual(number1, number2))
    {
        sign = -1;
        number1.swap(number2);
        number1.insert(number1.begin(), '0');
    }

    // 逐位相减,不够的向高位借位。
    int borrow = 0, i = number1.length() - 1, j = number2.length() - 1;
    for (; i >= 0; i--, j--)
    {
        int diff = number1[i] - '0' - (j >= 0 ? (number2[j] - '0') : 0) - borrow;
        borrow = 0;
        if (diff < 0)
        {
            diff += BASE;
            borrow = 1;
        }
        number1[i] = diff + '0';
    }

    // 移除前导0。
    zeroJustify(number1);

    // 设置计算结果的符号位。
    if (sign == -1)
        number1.insert(number1.begin(), '-');

    return number1;
}
Example #5
0
    /**
     * Implements plain-old positive integer multiplication.
     */
    string multiply(string num1, string num2) {
        // make sure num1 is longer
        if(num1.length() < num2.length())
            num1.swap(num2);

        list<int> value, temp_value;
        value.push_back(0); // initialize to 0
        for(int i=num2.length()-1;i >= 0;--i) {
            temp_value.clear();

            int d = num2[i] - '0', carry = 0, product;
            if(d == 0) continue;

            // "shifting" 
            for(int j=num2.length()-i-1;j > 0;--j) temp_value.push_back(0);

            for(int j=num1.length()-1;j >= 0;--j) {
                product = (num1[j]-'0') * d + carry;
                temp_value.push_back(product % 10);
                carry = product / 10;
            }

            if(carry) temp_value.push_back(carry);

            add_numbers(value, temp_value);
        }

        stringstream ss;
        for(auto it = value.rbegin();it != value.rend();++it)
            ss << *it;

        return ss.str();
    }
Example #6
0
    string add(string num1, string num2) {  // 对两个string相加
        reverse(num1.begin(),num1.end());
        reverse(num2.begin(),num2.end());
        
        if(num1.size() < num2.size()) num1.swap(num2);
        int cflag = 0;

        int i = 0;
        for(; i < num2.size(); ++i) {
            int temp = (num2[i] - '0')+(num1[i]-'0')+cflag;
            cflag = temp/10;
            num1[i] = temp%10 + '0';
        }
        
        while(cflag&&i<num1.size()) {
            int temp = num1[i]-'0'+cflag;
            cflag = temp/10;
            num1[i++] = temp%10 + '0';
        }
        
        if(cflag) num1.push_back(cflag+'0');
        
        reverse(num1.begin(),num1.end());
        
        return num1;
    }
Example #7
0
 string addBinary(string a, string b) {
     //swap a and b if a is shorter than b
     //we will store the answer in a
     if(a.length() < b.length())
         a.swap(b);
     //@l1 the index of a[]
     //@l2 the index of b[]
     int l1 = a.length()-1;
     int l2 = b.length()-1;
     int add = 0;
     //process the common bits
     for(; l1>=0 && l2>=0; --l1,--l2)
         {
             int c = a[l1]-'0';
             int d = b[l2]-'0';
             int e = c+d+add;
             a[l1] = e%2+'0';
             add = e/2;
         }
     //process the last bits in a
     for(; l1>=0; --l1)
         {
             int c = a[l1]-'0';
             int e = c+add;
             a[l1] = e%2+'0';
             add = e/2;
         }
     //insert an 1 at the first position
     if(1==add)
         a.insert(0,1,'1');
     return a;
 }
Example #8
0
        /**
         *  Finds the nth entry from the fasta file and returns the associated sequence
         **/
        static int getEntryFromFasta(const path& fasta_path, uint32_t n, string& header, string& sequence)
        {
            // Setup stream to sequence file and check all is well
            std::ifstream inputStream (fasta_path.c_str());

            if (!inputStream.is_open())
            {
                std::cerr << "ERROR: Could not open the sequence file: " << fasta_path << endl;
                return 0;
            }


            std::string id;
            std::string seq;
            uint32_t i = 1;

            // Read a record
            while(inputStream.good() && readRecord(inputStream, id, seq) == 0)
            {
                if (i == n)
                {
                    inputStream.close();

                    header.swap(id);
                    sequence = seq;
                    return 0;
                }

                seq.clear();
                i++;
            }

            inputStream.close();
            return -1;
        }
		void swap(TreeItem& rhs) noexcept
		{
			using std::swap;
			strName.swap(rhs.strName);
			Last.swap(rhs.Last);
			swap(Depth, rhs.Depth);
		}
Example #10
0
void FeatureType::GetReadableName(string & name) const
{
  ParseCommon();

  BestMatchedLangNames matcher;
  ForEachName(matcher);

  if (!matcher.m_nativeName.empty())
    name.swap(matcher.m_nativeName);
  else if (!matcher.m_defaultName.empty())
    name.swap(matcher.m_defaultName);
  else if (!matcher.m_intName.empty())
    name.swap(matcher.m_intName);
  else
    name.swap(matcher.m_englishName);
}
Example #11
0
		void swap(fmask& rhs) noexcept
		{
			using std::swap;
			swap(Used, rhs.Used);
			strMask.swap(rhs.strMask);
			FilterMask.swap(rhs.FilterMask);
		}
Example #12
0
// 将两个字符串形式表示的非负整数进行相加操作,返回字符串表示的结果。
string add(string number1, string number2)
{
    // 将结果保存在字符串number1中,为了相加方便,调整加数,使得第一个加数的数位
    // 总是大于第二个加数的数位。由于两个正数相加,和的数位最多为两个加数数位较大值
    // 加一,可以预先分配存储空间以方便计算。
    if (number1.length() < number2.length())
        number1.swap(number2);
    number1.insert(number1.begin(), '0');

    // 相加时从低位开始加。初始时进位为0。由于字符串中保存的是数位的ASCII码字符,
    // 需要做相应的转换,使之成为对应的数字值。当前的数位为模基数的值,进位则为除
    // 基数的值。
    int carry = 0, i = number1.length() - 1, j = number2.length() - 1;
    for (; i >= 0; i--, j--)
    {
        int sum = number1[i] - '0' + (j >= 0 ? (number2[j] - '0') : 0) + carry;
        number1[i] = sum % BASE + '0';
        carry = sum / BASE;
    }

    // 移除前导0。
    zeroJustify(number1);

    return number1;
}
// inspired from <Programming Pearls> -- Handwaving
void reverseWords2(string &s) {
    if (s.length() == 0) return;

    string result = "";
    if (s[s.length()-1] == ' ') {
        int last = s.find_last_not_of(' ') + 1;
        s.erase(last, s.length() - last);
    }

    int first = s.find_first_not_of(' ', 0);
    while (first != string::npos) {
        int wend = s.find(' ', first);  // word end
        if (wend == string::npos) wend = s.length();

        string word = s.substr(first, wend - first);
        reverse(word.begin(), word.end());
        result += word;

        first = s.find_first_not_of(' ', wend); // next word
        if (first == string::npos) break;

        result += ' ';
    }
    reverse(result.begin(), result.end());
    s.swap(result);
}
Example #14
0
 string addBinary(string a, string b) {
     if(a.length() < b.length())
         a.swap(b);
     
     findSum(a, b);
     
     return a;
 }
int PathResolution::followSymlink(string& path)
{
    if(++linksFollowed>=maxLinkToFollow) return -ELOOP;
    string target;
    {
        StringPart sp(path,index-1,indexIntoFs);
        if(int res=fs->readlink(sp,target)<0) return res;
    }
    if(target.empty()) return -ENOENT; //Should not happen
    if(target[0]=='/')
    {
        //Symlink is absolute
        size_t newPathLen=target.length()+path.length()-index+1;
        if(newPathLen>PATH_MAX) return -ENAMETOOLONG;
        string newPath;
        newPath.reserve(newPathLen);
        newPath=target;
        if(index<=path.length())
            newPath.insert(newPath.length(),path,index-1,string::npos);
        path.swap(newPath);
        fs=root;
        syms=root->supportsSymlinks();
        index=1;
        indexIntoFs=1;
        depthIntoFs=1;
    } else {
        //Symlink is relative
        size_t removeStart=path.find_last_of('/',index-2);
        size_t newPathLen=path.length()-(index-removeStart-2)+target.length();
        if(newPathLen>PATH_MAX) return -ENAMETOOLONG;
        string newPath;
        newPath.reserve(newPathLen);
        newPath.insert(0,path,0,removeStart+1);
        newPath+=target;
        if(index<=path.length())
            newPath.insert(newPath.length(),path,index-1,string::npos);
        path.swap(newPath);
        index=removeStart+1;
        depthIntoFs--;
    }
    return 0;
}
	void reverseWords(string &s)
	{
		if (s == "")
			return;

		string strTemp = s;
		stack<string> subStrings;
		size_t beginPos;
		size_t endPos;
		size_t subStrLength;

		do
		{
			beginPos = strTemp.find_first_not_of(' ');
			if (beginPos != string::npos)
			{
				strTemp = strTemp.substr(beginPos);
				beginPos = 0;
				endPos = strTemp.find_first_of(' ');
				if (endPos != string::npos)
				{
					subStrLength = endPos - beginPos;
					subStrings.push(strTemp.substr(beginPos, subStrLength));
					strTemp = strTemp.substr(endPos + 1);
				}
				else
				{
					subStrLength = strTemp.size() - beginPos;
					subStrings.push(strTemp.substr(beginPos, subStrLength));
					strTemp = strTemp.substr(strTemp.size());
				}
			}
			else
			{
				strTemp = "";
			}
		} while (!strTemp.empty());

		strTemp = "";
		while (!subStrings.empty())
		{
			string strTop = subStrings.top();
			strTemp.append(strTop);
			strTemp.append(" ");
			subStrings.pop();
		}

		if (strTemp != "")
		{
		    strTemp.pop_back();
		}

		s.swap(strTemp);
	}
void replaceAll(string& source, const string& from, const string& to) {
  string newString;
  newString.reserve(source.length());
  string::size_type lastPos = 0;
  string::size_type findPos;
  while(string::npos != (findPos = source.find(from, lastPos))) {
    newString.append( source, lastPos, findPos - lastPos );
    newString += to;
    lastPos = findPos + from.length();
  }
  newString += source.substr( lastPos );
  source.swap( newString );
}
Example #18
0
File: same.cpp Project: mazonka/w
void cfn_read(std::istream & is, string & dest)
{
    const int BSZ = 1000;

    char buf[BSZ + 1];

    is.read(buf, BSZ);

    auto sz = is.gcount();
    string x(buf, sz);

    dest.swap(x);
}
Example #19
0
bool EasyUtil::Base64Encode(const std::string &sInput, string &sOutput)
{
	std::string temp;
	temp.resize(modp_b64_encode_len(sInput.size()));  // makes room for null byte

													 // null terminates result since result is base64 text!
	int input_size = static_cast<int>(sInput.size());
	int output_size = modp_b64_encode(&(temp[0]), sInput.data(), input_size);
	if (output_size < 0)
		return false;

	temp.resize(output_size);  // strips off null byte
	sOutput.swap(temp);
	return true;
}
Example #20
0
void xmlEncode(string& data) {
    std::string buffer;
    buffer.reserve(data.size());
    for(size_t pos = 0; pos != data.size(); ++pos) {
        switch(data[pos]) {
            case '&':  buffer.append("&amp;");       break;
            case '\"': buffer.append("&quot;");      break;
            case '\'': buffer.append("&apos;");      break;
            case '<':  buffer.append("&lt;");        break;
            case '>':  buffer.append("&gt;");        break;
            default:   buffer.append(&data[pos], 1); break;
        }
    }
    data.swap(buffer);
}
Example #21
0
bool EasyUtil::Base64Decode(const std::string &sInput, string &sOutput)
{
	std::string temp;
	temp.resize(modp_b64_decode_len(sInput.size()));

	// does not null terminate result since result is binary data!
	int input_size = static_cast<int>(sInput.size());
	int output_size = modp_b64_decode(&(temp[0]), sInput.data(), input_size);
	if (output_size < 0)
		return false;

	temp.resize(output_size);
	sOutput.swap(temp);

	return true;
}
Example #22
0
void replaceAll(string& str, const string& from, const string& to) 
{
	if (from.empty())
		return;
	string wsRet;
	wsRet.reserve(str.length());
	size_t start_pos = 0, pos;
	while ((pos = str.find(from, start_pos)) != string::npos) {
		wsRet += str.substr(start_pos, pos - start_pos);
		wsRet += to;
		pos += from.length();
		start_pos = pos;
	}
	wsRet += str.substr(start_pos);
	str.swap(wsRet); // faster than str = wsRet;
}
Example #23
0
string Add(string fNum,string sNum)
{
        if(fNum.length()<sNum.length()) fNum.swap(sNum);
        string Add("0");
    Add+=fNum;
        for(unsigned i=1;i<=fNum.length();i++)
                if(i<=sNum.length())
                        Add[Add.length()-i]+=sNum[sNum.length()-i]-'0';
        for(unsigned i=1;i<Add.length();i++)
        {
                if(Add[Add.length()-i]>'9')
                { Add[Add.length()-i]-=10;Add[Add.length()-i-1]+=1; }
        }
    while(Add[0]=='0') Add.erase(0,1);
        return Add;
}
Example #24
0
bool session_cookies::load(session_interface *session,string &data,time_t &timeout_out)
{
	string cdata=session->get_session_cookie();
	if(cdata.empty()) return false;
	time_t timeout;
	string tmp;
	if(!encr->decrypt(cdata,tmp,&timeout))
		return false;
	time_t now;
	time(&now);
	if(timeout < now)
		return false;
	data.swap(tmp);
	timeout_out=timeout;
	return true;
}
Example #25
0
	void reverseWords(string &s) {
		const char* buf = s.c_str();
		string tmp;
		for (std::size_t sPos = 0; sPos < s.length(); ++sPos)
		{
			if (buf[sPos] == ' ') continue;
			std::size_t ePos = sPos + 1;
			while (ePos < s.length() && buf[ePos] != ' ')
				++ePos;

			tmp = tmp.empty() 
				? string(&buf[sPos], ePos - sPos)
				: string(&buf[sPos], ePos - sPos) + ' ' + tmp;
			sPos = ePos;
		}
		s.swap(tmp);
	}
Example #26
0
int create_msg(string& req, string command, string separator, string service_ip, int service_port, string srv_sgntr){
  	
	if (!command.empty()){
	    req.swap(command);
	    req+=separator;
	}
	
	req+=service_ip;
	req+=separator;
	req+=convertInt(service_port);
	req+=separator;
	req+=srv_sgntr;
	cout<<"create_msg crea: "<<req<<endl;
	
	return 0;
  
}
Example #27
0
bool session_cookies::load(session_interface &session,string &data,time_t &timeout_out)
{
	string cdata=session.get_session_cookie();
	if(cdata.empty()) return false;
	if(cdata[0]!='C') {
		session.clear_session_cookie();
		return false;
	}
	time_t timeout;
	string tmp;
	if(!encryptor_->decrypt(cdata.substr(1),tmp,&timeout))
		return false;
	if(timeout < time(0))
		return false;
	data.swap(tmp);
	timeout_out=timeout;
	return true;
}
Example #28
0
 string add(string num1, string num2) {
     if (num1.length() < num2.length()) {
         num1.swap(num2);
     }
     char c = '0';
     string result = "";
     int len1 = num1.length(), len2 = num2.length();
     for (int i = 0; i < len2; i ++) {
         char a = num1[len1 - i - 1], b = num2[len2 - i - 1];
         result = add(a, b, c) + result;
     }
     for (int i = len1 - len2 - 1; i >= 0; i --) {
         result = add(num1[i], '0', c) + result;
     }
     if (c != '0') {
         result = c + result;
     }
     return result;
 }
Example #29
0
int common_substr_len(string str1, string str2)
{
	string::size_type minLen;
	if (str1.length() < str2.length()) 
	{
		minLen = str1.length();
	}
	else
	{
		minLen = str2.length();
		str1.swap(str2); //make str1 the shorter string
	}

	string::size_type maxSubstrLen = 0;
	string::size_type posBeg;
	string::size_type substrLen;
	string sub;
	for (posBeg = 0; posBeg < minLen; posBeg++) 
	{
		for (substrLen = minLen-posBeg; substrLen > 0; substrLen--) 
		{
			sub = str1.substr(posBeg, substrLen);
			if (str2.find(sub) != string::npos) 
			{
				if (maxSubstrLen < substrLen) 
				{
					maxSubstrLen = substrLen;
				}
				
				if (maxSubstrLen >= minLen-posBeg-1) 
				{
					return maxSubstrLen;
				}				
			}
		}		
	}
	return 0;
}
Example #30
0
    string addBinary(string a, string b) {
        int sign = 0, cur_val = 0;
        string result = "";
        if (a.length() > b.length()) {
            a.swap(b);
        }
        int indexa = a.length() - 1;
        int indexb = b.length() - 1;
        for (; indexa >= 0; --indexa, --indexb) {
            cur_val = a.at(indexa) + b.at(indexb) - '0' - '0' + sign;
            sign = cur_val >> 1;
            result = string(1, (cur_val & 0x1) + '0') + result;
        }
        for (; indexb >= 0; --indexb) {
            cur_val = b.at(indexb) - '0' + sign;
            sign = cur_val >> 1;
            result = string(1, (cur_val & 0x1) + '0') + result;
        }
        if (sign > 0) {
            result = string(1, '1') + result;
        }

        return ((result.length() > 0) ? result : "0");
    }