C++ 实现类似Python中format函数

Python:

C++

代码:

_StrW _StrW::Python_format(const _StrListW& pArgList) const
{
    _StrW sResult;

    //设置缓冲区
    sResult.SetBuffer(_nLength + pArgList.GetStringLength());

    int iCount = 0;

    for (int i = 0; i < _nLength; ++i)
    {
        if (_pData[i] == _t('{'))
        {
            //查找第二个配对
            int iEnd = this->IndexOfRightPairChar(i, _t('{'), _t('}'));

            if (iEnd != -1)
            {
                //获取 {  }  中的数字字符
                _string sCount = SubStr(i, iEnd - i).GetAllArabicDigit();

                if (sCount.Length == 0)   //自动选择参数
                { 

                    if (iCount < pArgList.Count)
                    {
                        sResult.Add(pArgList[iCount]);
                    } 
                    else
                    {
                        sResult.Add(_t("{ NULL }"));
                    }
                    ++iCount; //参数计数             
                }
                else
                {
                    int n = _Math::StrToInt_t<_char>(sCount.Data); //指定参数
                     
                    if (n < pArgList.Count)
                    {
                        sResult.Add(pArgList[n]);
                    }
                    else
                    {
                        sResult.Add(_t("{ NULL }"));
                    }
                }

                i = iEnd;  //跳过 {??}  参数
            }

        }
        else
        {
            sResult.Add(_pData[i]);
        }
   
    }

    return sResult;
}

_StrW _StrW::Python_format(const _StrW& sArg, const _StrW& sSplit) const
{
    return Python_format(_StrListW(sArg, sSplit,true));
}

    /// <summary>
    /// 返回右边配对字符的位置
    /// </summary>
    /// <param name="sText"></param>
    /// <param name="nLeftPos"></param>
    /// <param name="cLeft"></param>
    /// <returns></returns>
    /// 创建时间:  2022-12-18      最后一次修改时间:2022-12-18   已测试(2022-12-18)
    int IndexOfRightPairChar(const int nLeftPos, const _char cLeft, const _char cRight)const
    {
        return gs.s_FindRightPairChar_t<T>(_pData, nLeftPos, cLeft, cRight);
    }

    /// <summary>
    /// 向后查找右边配对的字符,例如 { } [ ] 
    /// </summary>
    /// <typeparam name="T">字符串类型</typeparam>
    /// <param name="sText">文本</param>
    /// <param name="nLeftPos">第一个字符出现的位置</param>
    /// <param name="cLeft">第一个字符</param>
    /// <param name="cRight">第二个字符</param>
    /// <returns></returns>
    template<class T>
    static int s_FindRightPairChar_t(const T* sText, const int nLeftPos, const T& cLeft, const T& cRight)
    {
        int iFlag = 0;

        int n = nLeftPos + 1;

        while (sText[n] != 0)
        {
            T c = sText[n];

            if (c == cRight)
            {
                if (iFlag == 0) { return n; }
                --iFlag;
            }
            else if (c == cLeft)
            {
                ++iFlag;
            }
            ++n;
        }
        return -1;
    }

    /// <summary>
    /// 返回所有的数字
    /// </summary>
    /// <returns></returns>
    /// 创建时间: 2023-02-18      最后一次修改时间:2023-31-19
    inline _Str<T> GetAllArabicDigit()const
    {
        _Str<T> sResult;
        sResult.SetBuffer(_nLength);

        for (int i = 0; i < _nLength; ++i)
        {
            T c = _pData[i];
            if (gcf.gcf_isdigit(c))
            {
                sResult.Add(c);
            }
        }

        return sResult;
    }

示例:

代码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值