From 0227fe9d6c2c5ee838a53489339ff32d60584e69 Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Wed, 21 Oct 2015 19:42:14 -0300 Subject: [PATCH] Adds support to t:compressWhitespace --- .../transformations/compress_whitespace.cc | 30 +++++++++++++------ src/actions/transformations/transformation.cc | 2 ++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/actions/transformations/compress_whitespace.cc b/src/actions/transformations/compress_whitespace.cc index d7cbabd7..a971cdfd 100644 --- a/src/actions/transformations/compress_whitespace.cc +++ b/src/actions/transformations/compress_whitespace.cc @@ -37,16 +37,28 @@ CompressWhitespace::CompressWhitespace(std::string action) std::string CompressWhitespace::evaluate(std::string value, Assay *assay) { - /** - * @todo Implement the transformation CompressWhitespace - */ - if (assay) { -#ifndef NO_LOGS - assay->debug(4, "Transformation CompressWhitespace is " \ - "not implemented yet."); -#endif + + std::string a; + int inWhiteSpace = 0; + int i = 0; + + while (i < value.size()) { + if (isspace(value[i])) { + if (inWhiteSpace) { + i++; + continue; + } else { + inWhiteSpace = 1; + a.append(" ", 1); + } + } else { + inWhiteSpace = 0; + a.append(&value.at(i), 1); + } + i++; } - return value; + + return a; } } // namespace transformations diff --git a/src/actions/transformations/transformation.cc b/src/actions/transformations/transformation.cc index 430d922e..043ccf22 100644 --- a/src/actions/transformations/transformation.cc +++ b/src/actions/transformations/transformation.cc @@ -45,6 +45,7 @@ #include "actions/transformations/remove_comments.h" #include "actions/transformations/remove_nulls.h" #include "actions/transformations/remove_whitespace.h" +#include "actions/transformations/compress_whitespace.h" #include "actions/transformations/replace_comments.h" #include "actions/transformations/replace_nulls.h" #include "actions/transformations/sha1.h" @@ -96,6 +97,7 @@ Transformation* Transformation::instantiate(std::string a) { IF_MATCH(remove_comments) { return new RemoveComments(a); } IF_MATCH(removeNulls) { return new RemoveNulls(a); } IF_MATCH(remove_whitespace) { return new RemoveWhitespace(a); } + IF_MATCH(compressWhitespace) { return new CompressWhitespace(a); } IF_MATCH(replace_comments) { return new ReplaceComments(a); } IF_MATCH(replace_nulls) { return new ReplaceNulls(a); } IF_MATCH(sha1) { return new Sha1(a); }