gpt4 book ai didi

c++ - 在 C/C++ 中使用空格作为分隔符将字符串拆分为字符串数组的更好方法

转载 作者:IT老高 更新时间:2023-10-28 22:35:10 25 4
gpt4 key购买 nike

抱歉,我的 C/C++ 不太好,但以下现有代码即使对我来说也像是垃圾。它还有一个错误 - 当 str = "07/02/2010"以 '\0' 终止时失败 - 。我认为与其修复错误,不如重写它。在 Python 中,它只是 'kas\nhjkfh kjsdjkasf'.split()。我知道这是 C-ish 代码,但拆分字符串不会那么复杂!坚持相同的签名,并且不使用额外的库,我该如何改进它 - 让它简短而甜蜜?我可以看出这段代码有异味,例如因为结尾处的 else 子句。

失败的线路:

_tcsncpy_s(
s.GetBuffer((int) (nIndex-nLast)),
nIndex-nLast,
psz+nLast,
(size_t) (nIndex-nLast)
);

如果字符串“07/02/2010”以 '\0' 结尾,它将尝试将 11 个字符写入一个只有 10 个字符长的缓冲区。

功能齐全:

#define 

// This will return the text string as a string array
// This function is called from SetControlText to parse the
// text string into an array of CStrings that the control
// Gadgets will attempt to interpret

BOOL CLVGridDateTimeCtrl::ParseTextWithCurrentFormat(const CString& str, const CGXStyle* pOldStyle, CStringArray& strArray )
{
// Unused:
pOldStyle;

// we assume that the significant segments are seperated by space

// Please change m_strDelim to add other delimiters

CString s;

LPCTSTR psz = (LPCTSTR) str;

BOOL bLastCharSpace = FALSE;
DWORD size = str.GetLength()+1;

// (newline will start a new row, tab delimiter will
// move to the next column).
// parse buffer (DBCS aware)
for (DWORD nIndex = 0, nLast = 0; nIndex < size; nIndex += _tclen(psz+nIndex))
{
// check for a delimiter
if (psz[nIndex] == _T('\0') || _tcschr(_T("\r\n"), psz[nIndex]) || _tcschr(_T(" "), psz[nIndex])
||!_tcscspn(&psz[nIndex], (LPCTSTR)m_strDelim))
{
s.ReleaseBuffer();
s.Empty();
// abort parsing the string if next char
// is an end-of-string
if (psz[nIndex] == _T('\0'))
{
if (psz[nIndex] == _T('\r') && psz[nIndex+1] == _T('\n'))
nIndex++;

_tcsncpy_s(s.GetBuffer((int) (nIndex-nLast)),
nIndex-nLast,
psz+nLast,
(size_t) (nIndex-nLast));
CString temStr = s;
strArray.Add(temStr);
temStr.Empty();
break;
}

else if (_tcscspn(&psz[nIndex], (LPCTSTR)m_strDelim) == 0 && !bLastCharSpace)
{
if (psz[nIndex] == _T('\r') && psz[nIndex+1] == _T('\n'))
nIndex++;

_tcsncpy_s(s.GetBuffer((int) (nIndex-nLast)),
nIndex-nLast,
psz+nLast,
(size_t) (nIndex-nLast));
CString temStr = s;
strArray.Add(temStr);
temStr.Empty();
bLastCharSpace = TRUE;
// abort parsing the string if next char
// is an end-of-string
if (psz[nIndex+1] == _T('\0'))
break;

}
// Now, that the value has been copied to the cell,
// let's check if we should jump to a new row.
else if (_tcschr(_T(" "), psz[nIndex]) && !bLastCharSpace)
{
if (psz[nIndex] == _T('\r') && psz[nIndex+1] == _T('\n'))
nIndex++;

_tcsncpy_s(s.GetBuffer((int) (nIndex-nLast)),
nIndex-nLast,
psz+nLast,
(size_t) (nIndex-nLast));
CString temStr = s;
strArray.Add(temStr);
temStr.Empty();
bLastCharSpace = TRUE;
// abort parsing the string if next char
// is an end-of-string
if (psz[nIndex+1] == _T('\0'))
break;
}

nLast = nIndex + _tclen(psz+nIndex);


}
else
{
// nLast = nIndex + _tclen(psz+nIndex);
bLastCharSpace = FALSE;
}
}
if (strArray.GetSize())
return TRUE;
else
return FALSE;
}

编辑:m_strDelim = _T(","); 并且这个成员变量只在这个函数中使用。我想我现在看到了标记化的意义——它试图解析日期和时间……等等,还有更多!下面是调用此函数的代码。请帮助我改善这一点。我的一些同事声称 C# 并不比 C++ 更高效。我曾经觉得自己像个白痴,因为不能对我说同样的话。

// SetControlText will attempt to convert the text to a valid date first with
// the help of COleDateTime and then with the help of the Date control and the
// current format

BOOL CLVGridDateTimeCtrl::ConvertControlTextToValue(CString& str, ROWCOL nRow, ROWCOL nCol, const CGXStyle* pOldStyle)
{
CGXStyle* pStyle = NULL;
BOOL bSuccess = FALSE;

if (pOldStyle == NULL)
{
pStyle = Grid()->CreateStyle();
Grid()->ComposeStyleRowCol(nRow, nCol, pStyle);
pOldStyle = pStyle;
}

// allow only valid input
{
// First do this
CLVDateTime dt;

if (str.IsEmpty())
{
;
// if (Grid()->IsCurrentCell(nRow, nCol))
// Reset();
bSuccess = TRUE;
}
else if (dt.ParseDateTime(str,CLVGlobals::IsUSDateFormat()) && (DATE) dt != 0)
{
SetDateTime(dt);
if (m_bDateValueAsNumber)
str.Format(_T("%g"), (DATE) dt);
else
str = dt.Format();
bSuccess = TRUE;
}
else
{
// parse the string using the current format
CStringArray strArray;
if (!ParseTextWithCurrentFormat(str, pOldStyle, strArray))
return FALSE;

UpdateNullStatus(m_TextCtrlWnd);

SetFormat(m_TextCtrlWnd, *pOldStyle);

int nArrIndex = 0;
for(int i=0; i<m_TextCtrlWnd.m_gadgets.GetSize(); i++)
{
int val = m_TextCtrlWnd.m_gadgets[i]->GetValue();
// s.Empty();
if(m_TextCtrlWnd.m_gadgets[i]->IsKindOf(RUNTIME_CLASS(SECDTNumericGadget)))
{
// TRACE(_T("The value %s\n"), strArray[nArrIndex]);
((CLVDTNumericGadget*)m_TextCtrlWnd.m_gadgets[i])->m_nNewValue = _ttoi(strArray[nArrIndex]);
nArrIndex++;
if (nArrIndex>strArray.GetUpperBound())
break;
}
else if(m_TextCtrlWnd.m_gadgets[i]->IsKindOf(RUNTIME_CLASS(SECDTListGadget)) && val!=-1)
{
int nIndex = ((CLVDTListGadget*)m_TextCtrlWnd.m_gadgets[i])->FindMatch(strArray[nArrIndex], ((CLVDTListGadget*)m_TextCtrlWnd.m_gadgets[i])->GetValue()+1);
if (nIndex!=-1)
{
// TRACE(_T("The value %s\n"), strArray[nArrIndex]);
((CLVDTListGadget*)m_TextCtrlWnd.m_gadgets[i])->SetValue(nIndex);
nArrIndex++;
if (nArrIndex>strArray.GetUpperBound())
break;
}

}

CLVDBValue dbDate = m_TextCtrlWnd.GetDateTime();
if (dbDate.IsNull())
str = _T("");
else
{
CLVDateTime dt = (CLVDateTime)dbDate;
if (m_bDateValueAsNumber)
str.Format(_T("%g"), (DATE) dt);
else
str = dt.Format();
}
}
bSuccess = TRUE;
}
}

if (pStyle)
Grid()->RecycleStyle(pStyle);

return bSuccess;
}

最佳答案

String Toolkit Library (Strtk)对您的问题有以下解决方案:

#include <string>
#include <deque>
#include "strtk.hpp"
int main()
{
std::string data("kas\nhjkfh kjsdjkasf");
std::deque<std::string> str_list;
strtk::parse(data, ", \r\n", str_list);
return 0;
}

更多示例请见 Here

关于c++ - 在 C/C++ 中使用空格作为分隔符将字符串拆分为字符串数组的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3162108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com