std::search matches start of data before passing to function, but not within function?
-
#ifndef BYTE // For OS X -ers
#define unsigned char BYTE
#define unsigned char * LPBYTE
#endif
#include <vector>
#include <algorithm>
void separate(vector< pair<LPBYTE, LPBYTE> >& retVec, LPBYTE data, LPBYTE delimeter, /* out */bool *bComplete = 0) {
retVec.clear();
if (bComplete) *bComplete = false;if (!data) // No data - leave retVec empty return; LPBYTE dataEnd = data + sizeof(data); LPBYTE delEnd = delimeter + sizeof(delimeter); size\_t delSize = distance(delimeter, delEnd); LPBYTE found = std::search(data, dataEnd, delimeter, delEnd); if (found == delEnd) // Delimeter not in data - leave retVec empty return; cout << "In separate(...)\\n"; if (data\[0\] == delimeter\[0\]) cout << "data\[0\] == delimeter\[0\]\\n"; if (found == data) cout << "Found == data.\\n(" << found\[0\] << " == " << data\[0\] << ")\\n"; if (found != data) // Found delimeter ends first split cout << "Found != data; found == data\[" << distance(data, found) << "\].\\n"; return;
};
int main()
{
BYTE buffer[] = {',', '1', '2', '3', ',', '4', '5', '6', ',', '7', '8', '9', ',', 0};
BYTE del = ',';
bool bComp = true;LPBYTE found = std::search(&buffer\[0\], &buffer\[0\] + sizeof(buffer), &del, &del + sizeof(del)); if (found == &buffer\[0\]) cout << "found == &buffer\[0\]\\n"; else cout << "found == &buffer\[" << distance(&buffer\[0\], found) << "\]\\n"; vector< pair<LPBYTE, LPBYTE> > lpb; separate(lpb, buffer, &del, &bComp); return 0;
}
Output:
found == &buffer[0]
In separate(...)
data[0] == delimeter[0]
Found != data; Found == data[4]When I
search
from withinmain()
, the leading comma is found, but once I pass the data & delimeter toseparate()
, it misses the leading comma and finds the next one.. obviously I'm missing something - can anyone spot what it is? Your assistance is greatly appreciated! MZR -
#ifndef BYTE // For OS X -ers
#define unsigned char BYTE
#define unsigned char * LPBYTE
#endif
#include <vector>
#include <algorithm>
void separate(vector< pair<LPBYTE, LPBYTE> >& retVec, LPBYTE data, LPBYTE delimeter, /* out */bool *bComplete = 0) {
retVec.clear();
if (bComplete) *bComplete = false;if (!data) // No data - leave retVec empty return; LPBYTE dataEnd = data + sizeof(data); LPBYTE delEnd = delimeter + sizeof(delimeter); size\_t delSize = distance(delimeter, delEnd); LPBYTE found = std::search(data, dataEnd, delimeter, delEnd); if (found == delEnd) // Delimeter not in data - leave retVec empty return; cout << "In separate(...)\\n"; if (data\[0\] == delimeter\[0\]) cout << "data\[0\] == delimeter\[0\]\\n"; if (found == data) cout << "Found == data.\\n(" << found\[0\] << " == " << data\[0\] << ")\\n"; if (found != data) // Found delimeter ends first split cout << "Found != data; found == data\[" << distance(data, found) << "\].\\n"; return;
};
int main()
{
BYTE buffer[] = {',', '1', '2', '3', ',', '4', '5', '6', ',', '7', '8', '9', ',', 0};
BYTE del = ',';
bool bComp = true;LPBYTE found = std::search(&buffer\[0\], &buffer\[0\] + sizeof(buffer), &del, &del + sizeof(del)); if (found == &buffer\[0\]) cout << "found == &buffer\[0\]\\n"; else cout << "found == &buffer\[" << distance(&buffer\[0\], found) << "\]\\n"; vector< pair<LPBYTE, LPBYTE> > lpb; separate(lpb, buffer, &del, &bComp); return 0;
}
Output:
found == &buffer[0]
In separate(...)
data[0] == delimeter[0]
Found != data; Found == data[4]When I
search
from withinmain()
, the leading comma is found, but once I pass the data & delimeter toseparate()
, it misses the leading comma and finds the next one.. obviously I'm missing something - can anyone spot what it is? Your assistance is greatly appreciated! MZRIn main(),
del
is of typeBYTE
andsizeof(del)
is 1. Once it's passed toseparate
,del
is of typeLPBYTE
andsizeof(del)
is 4.