mirror of https://github.com/vitalif/openscad
minor fixes: Don't copy input vectors, fixed a leak of a Value object + some cosmetics
parent
07f2da7968
commit
9d7446de46
118
src/func.cc
118
src/func.cc
|
@ -390,99 +390,95 @@ Value builtin_lookup(const Context *, const std::vector<std::string>&, const std
|
||||||
*/
|
*/
|
||||||
Value builtin_search(const Context *, const std::vector<std::string>&, const std::vector<Value> &args)
|
Value builtin_search(const Context *, const std::vector<std::string>&, const std::vector<Value> &args)
|
||||||
{
|
{
|
||||||
Value findThis;
|
if (args.size() < 2) return Value();
|
||||||
Value searchTable;
|
|
||||||
|
const Value &findThis = args[0];
|
||||||
|
const Value &searchTable = args[1];
|
||||||
|
unsigned int num_returns_per_match = (args.size() > 2) ? args[2].num : 1;
|
||||||
|
unsigned int index_col_num = (args.size() > 3) ? args[3].num : 0;
|
||||||
|
|
||||||
Value returnVector;
|
Value returnVector;
|
||||||
returnVector.type = Value::VECTOR;
|
returnVector.type = Value::VECTOR;
|
||||||
unsigned int num_returns_per_match = 1;
|
|
||||||
unsigned int index_col_num=0;
|
if (findThis.type == Value::NUMBER) {
|
||||||
if (args.size() < 2 )
|
unsigned int matchCount = 0;
|
||||||
return Value();
|
|
||||||
findThis=args[0];
|
|
||||||
// PRINTB(" builtin_search: findThis = %s",findThis);
|
|
||||||
searchTable=args[1];
|
|
||||||
// PRINTB(" builtin_search: searchTable = %s",searchTable);
|
|
||||||
if ( args.size() > 2 ) num_returns_per_match=args[2].num;
|
|
||||||
if ( args.size() > 3 ) index_col_num=args[3].num;
|
|
||||||
if ( findThis.type==Value::NUMBER ) {
|
|
||||||
// PRINTB(" builtin_search: findThis type: NUMBER %s",findThis);
|
|
||||||
unsigned int matchCount=0;
|
|
||||||
Value *resultVector = new Value();
|
Value *resultVector = new Value();
|
||||||
resultVector->type = Value::VECTOR;
|
resultVector->type = Value::VECTOR;
|
||||||
for (size_t j = 0; j < searchTable.vec.size(); j++) {
|
for (size_t j = 0; j < searchTable.vec.size(); j++) {
|
||||||
if ( searchTable.vec[j]->vec[index_col_num]->type==Value::NUMBER && findThis.num == searchTable.vec[j]->vec[index_col_num]->num ) {
|
if (searchTable.vec[j]->vec[index_col_num]->type == Value::NUMBER &&
|
||||||
Value *resultValue;
|
findThis.num == searchTable.vec[j]->vec[index_col_num]->num) {
|
||||||
resultValue = new Value(double(j));
|
returnVector.append(new Value(double(j)));
|
||||||
returnVector.append(resultValue);
|
|
||||||
matchCount++;
|
matchCount++;
|
||||||
if(num_returns_per_match!=0 && matchCount>=num_returns_per_match) break;
|
if (num_returns_per_match != 0 && matchCount >= num_returns_per_match) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ( findThis.type==Value::STRING ) {
|
} else if (findThis.type == Value::STRING) {
|
||||||
//PRINTB(" builtin_search: findThis type STRING %s",findThis);
|
|
||||||
//PRINTB(" builtin_search: checking findThis.text.size()==%s",findThis.text.size());
|
|
||||||
unsigned int searchTableSize;
|
unsigned int searchTableSize;
|
||||||
if(searchTable.type == Value::STRING) {
|
if (searchTable.type == Value::STRING) searchTableSize = searchTable.text.size();
|
||||||
searchTableSize=searchTable.text.size();
|
else searchTableSize = searchTable.vec.size();
|
||||||
} else {
|
|
||||||
searchTableSize=searchTable.vec.size();
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < findThis.text.size(); i++) {
|
for (size_t i = 0; i < findThis.text.size(); i++) {
|
||||||
unsigned int matchCount=0;
|
unsigned int matchCount = 0;
|
||||||
Value *resultVector = new Value();
|
Value *resultVector = new Value();
|
||||||
resultVector->type = Value::VECTOR;
|
resultVector->type = Value::VECTOR;
|
||||||
for (size_t j = 0; j < searchTableSize; j++) {
|
for (size_t j = 0; j < searchTableSize; j++) {
|
||||||
// PRINTB(" builtin_search: checking findThis.text[i]==%s",findThis.text[i]);
|
if (searchTable.type == Value::VECTOR &&
|
||||||
if ( searchTable.type==Value::VECTOR && findThis.text[i] == searchTable.vec[j]->vec[index_col_num]->text[0]
|
findThis.text[i] == searchTable.vec[j]->vec[index_col_num]->text[0] ||
|
||||||
|| searchTable.type==Value::STRING && findThis.text[i] == searchTable.text[j]
|
searchTable.type == Value::STRING &&
|
||||||
) {
|
findThis.text[i] == searchTable.text[j]) {
|
||||||
Value *resultValue;
|
Value *resultValue = new Value(double(j));
|
||||||
resultValue = new Value(double(j));
|
|
||||||
matchCount++;
|
matchCount++;
|
||||||
if(num_returns_per_match==1) {
|
if (num_returns_per_match==1) {
|
||||||
returnVector.append(resultValue);
|
returnVector.append(resultValue);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
resultVector->append(resultValue);
|
resultVector->append(resultValue);
|
||||||
}
|
}
|
||||||
if(num_returns_per_match>1 && matchCount>=num_returns_per_match) break;
|
if (num_returns_per_match > 1 && matchCount >= num_returns_per_match) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(matchCount==0) PRINTB(" search term not found: \"%s\"",findThis.text[i]);
|
if (matchCount == 0) PRINTB(" search term not found: \"%s\"", findThis.text[i]);
|
||||||
if(num_returns_per_match==0 || num_returns_per_match>1) returnVector.append(resultVector);
|
if (num_returns_per_match == 0 || num_returns_per_match > 1) {
|
||||||
|
returnVector.append(resultVector);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
;
|
} else if (findThis.type == Value::VECTOR) {
|
||||||
} else if ( findThis.type==Value::VECTOR ) {
|
|
||||||
// PRINTB(" builtin_search: findThis type: VECTOR %s",findThis);
|
|
||||||
for (size_t i = 0; i < findThis.vec.size(); i++) {
|
for (size_t i = 0; i < findThis.vec.size(); i++) {
|
||||||
unsigned int matchCount=0;
|
unsigned int matchCount = 0;
|
||||||
Value *resultVector = new Value();
|
Value *resultVector = new Value();
|
||||||
resultVector->type = Value::VECTOR;
|
resultVector->type = Value::VECTOR;
|
||||||
for (size_t j = 0; j < searchTable.vec.size(); j++) {
|
for (size_t j = 0; j < searchTable.vec.size(); j++) {
|
||||||
Value *resultValue;
|
if (findThis.vec[i]->type == Value::NUMBER &&
|
||||||
resultValue = new Value(double(j));
|
searchTable.vec[j]->vec[index_col_num]->type == Value::NUMBER &&
|
||||||
if ( findThis.vec[i]->type==Value::NUMBER && searchTable.vec[j]->vec[index_col_num]->type==Value::NUMBER && findThis.vec[i]->num == searchTable.vec[j]->vec[index_col_num]->num
|
findThis.vec[i]->num == searchTable.vec[j]->vec[index_col_num]->num ||
|
||||||
|| findThis.vec[i]->type==Value::STRING && searchTable.vec[j]->vec[index_col_num]->type==Value::STRING && findThis.vec[i]->text == searchTable.vec[j]->vec[index_col_num]->text ) {
|
findThis.vec[i]->type == Value::STRING &&
|
||||||
resultValue = new Value(double(j));
|
searchTable.vec[j]->vec[index_col_num]->type == Value::STRING &&
|
||||||
|
findThis.vec[i]->text == searchTable.vec[j]->vec[index_col_num]->text) {
|
||||||
|
Value *resultValue = new Value(double(j));
|
||||||
matchCount++;
|
matchCount++;
|
||||||
if(num_returns_per_match==1) {
|
if (num_returns_per_match==1) {
|
||||||
returnVector.append(resultValue);
|
returnVector.append(resultValue);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
resultVector->append(resultValue);
|
resultVector->append(resultValue);
|
||||||
}
|
}
|
||||||
if(num_returns_per_match>1 && matchCount>=num_returns_per_match) break;
|
if (num_returns_per_match > 1 && matchCount >= num_returns_per_match) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( num_returns_per_match==1 && matchCount==0 ) {
|
if (num_returns_per_match == 1 && matchCount == 0) {
|
||||||
if(findThis.vec[i]->type==Value::NUMBER) PRINTB(" search term not found: %s",findThis.vec[i]->num);
|
if (findThis.vec[i]->type == Value::NUMBER) {
|
||||||
if(findThis.vec[i]->type==Value::STRING) PRINTB(" search term not found: \"%s\"",findThis.vec[i]->text);
|
PRINTB(" search term not found: %s",findThis.vec[i]->num);
|
||||||
|
}
|
||||||
|
else if (findThis.vec[i]->type == Value::STRING) {
|
||||||
|
PRINTB(" search term not found: \"%s\"",findThis.vec[i]->text);
|
||||||
|
}
|
||||||
returnVector.append(resultVector);
|
returnVector.append(resultVector);
|
||||||
}
|
}
|
||||||
if(num_returns_per_match==0 || num_returns_per_match>1) returnVector.append(resultVector);
|
if (num_returns_per_match == 0 || num_returns_per_match > 1) {
|
||||||
|
returnVector.append(resultVector);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PRINTB(" search: none performed on input %s",findThis);
|
PRINTB(" search: none performed on input %s", findThis);
|
||||||
return Value();
|
return Value();
|
||||||
}
|
}
|
||||||
return returnVector;
|
return returnVector;
|
||||||
|
|
Loading…
Reference in New Issue