std::string ReverseComplement(const std::string& seq) {
  std::string output;
  output.reserve(seq.length());
  for (auto it = seq.crbegin(); it != seq.crend(); ++it) {
    auto bp = *it;
    switch(bp) {
      case 'A':
        output.push_back('T');
        break;
      case 'C':
        output.push_back('G');
        break;
      case 'G':
        output.push_back('C');
        break;
      case 'T':
        output.push_back('A');
        break;
      case '-':
        output.push_back('-');
        break;
      default:
        Rcpp::stop("Tried to reverse complement sequence with non ATGC-");
    }
  }
  return output;
}
Example #2
0
std::string trim_copy(const std::string& s)
{
  auto ch = s.cbegin();
  auto rch = s.crbegin();
  for(; *ch == ' ' || *ch == '\n' || *ch == '\t'; ++ch);
  for(; *rch == ' ' || *rch == '\n' || *rch == '\t'; ++rch);
  return std::string(ch, rch.base());
}
	inline void OpticalFlowApp::initOutputDirectory(const std::string& dir_name)
	{
		namespace bf = boost::filesystem;
		out_dir_ = *dir_name.crbegin() == '/' ? dir_name : dir_name + "/";
		bf::create_directories(out_dir_ + "camera/raw");
		bf::create_directory(out_dir_ + "projection");
		bf::create_directory(out_dir_ + "flow_data");
		return;
	}
Example #4
0
bool is_palindrome(std::string word){
	if(word.size() % 2) return false;

	boost::algorithm::to_lower(word);

	std::string reversedWord{word.crbegin(), word.crend()};

	return word == reversedWord;

}
/// <summary>
/// Reverses the given std::string.
/// </summary>
/// <param name="original_str">The given std::string</param>
/// <returns>A reverse std::string of the original std::string</returns>
std::string StringFunctions::reverse(const std::string &original_str)
{
	std::string working_str = "";
	for (std::string::const_reverse_iterator itr = original_str.crbegin(); itr != original_str.crend(); itr++)
	{
		working_str += *itr;
	}
	
	return working_str;
}
Example #6
0
bool isPalindrom(const std::string& str) {

  auto itr = str.cbegin();
  auto rev_itr = str.crbegin();
  int i = std::floor(str.size() / 2);
  while ((i--)) {
    if (*(itr++) != *(rev_itr++))
      return false;
  }
  return true;
}
Example #7
0
    string_view_t match(const string_view_t & numview) const {
        string_view_t empty_result {numview.str, numview.pos_end, numview.pos_end};
        if (   (is_terminal_rule() && !numview.right_aligned())
            || (is_start_rule()    && !numview.leftt_aligned())  )
            return empty_result;

        auto inumber = numview.crbegin();
        for (auto ipattern = pattern.crbegin(); ipattern != pattern.crend(); ++ipattern) {
            if (::isspace(*ipattern) || *ipattern == '.' || *ipattern == '$') continue;
            if (inumber == numview.crend() || (*ipattern == '#' && *inumber == '?'))  return empty_result;
            if (*ipattern == '#') break;
            if (*inumber != *ipattern) return empty_result;
            ++inumber;
        }
        if (is_start_rule() && inumber != numview.crend()) return empty_result;
        return string_view_t{empty_result}.set_begin(inumber);
    }
Example #8
0
BigInt::BigInt(std::string s)
	: sign(Sign::positive)
{
	if (s == "") {
		BigInt::BigInt();
		return;
	}

	if (s[0] == '-') { // handle the leading '-' sign
		sign = Sign::negative;
		s.erase(0, 1); // leave only numbers in the string
	}
	// need to read number is reverse order since that's how they are stored
	for (auto it = s.crbegin(); it != s.crend(); ++it) {
		if (isdigit(*it))
			digits.push_back((*it) - '0');
	}

}
Example #9
0
std::string insert_finjector::inject(const std::string& chunk) {
  std::string output;
  auto out_it = std::back_inserter(output);
  auto first = chunk.cbegin();
  auto end = chunk.cend();

  if (skip_ws_) {
    // Find the first and last non ws char
    first = std::find_if(first, end, isgraph);
    end = std::find_if(chunk.crbegin(), chunk.crend(), isgraph).base();

    // Still need to copy all the leading ws to the output
    std::copy(chunk.cbegin(), first, out_it);
  }

  if (position_ & position::begining) {
		for (size_t i = 0; i < n_; ++i)
			std::copy(insert_.cbegin(), insert_.cend(), out_it);
	}

  if (position_ & position::middle) {
    auto mid = first + (end - first) / 2;
    std::copy(first, mid, out_it);
		for (size_t i = 0; i < n_; ++i)
			std::copy(insert_.cbegin(), insert_.cend(), out_it);
    std::copy(mid, end, out_it);
  }
  else
    std::copy(first, end, out_it);

  if (position_ & position::end) {
		for (size_t i = 0; i < n_; ++i)
			std::copy(insert_.cbegin(), insert_.cend(), out_it);
	}

  // In case there was some ws we skipped at the end, add them last
  std::copy(end, chunk.cend(), out_it);

  return output;
}
Example #10
0
void Number::fromString(const std::string &input)
{
    m_decimals = 0;
    m_digits.clear();
    m_negative = false;

    int numbersAdded = 0;

    for (auto it = input.crbegin(); it != input.crend(); it++) {
        if (*it == '-' || *it == '+') {
            if (numbersAdded == 0)
                throw Exception("Invalid number: Sign before any digit read");

            m_negative = (*it == '-');
            break;
        }

        if (*it == '.' || *it == ',') {
            if (m_decimals != 0)
                throw Exception("Invalid number: Multiple decimal seperators found");

            m_decimals = numbersAdded;
            continue;
        }

        if (*it < '0' || *it > '9')
            throw Exception("Invalid number: Unknown character found");

        m_digits.push_back(*it - '0');
        ++numbersAdded;
    }

    m_trailingZeros = normalize();

    m_precision = m_decimals > 9 ? m_decimals : 10;
}
Example #11
0
CNum::Counter::Counter(const std::string &s) : value() {
    if (s.size() == 0) {
        setZero();
        return;
    }

    // Parse the correct base
    auto rBegin = s.crbegin();
    auto rEnd = s.crend();
    Unit base = 10;
    if (s.size() >= 2 && s[0] == '0') {
        if (s[1] == 'x') {
            base = 16;
            rEnd -= 2;
        } else if (s[1] == 'b') {
            base = 2;
            rEnd -= 2;
        }
    }

    // rBegin and rEnd is set
    if (base == 16) {

        // 1 character in hex occupied 4 bits

        Index shift = 0;
        Unit unit = 0;
        for (auto itr = rBegin; itr != rEnd; itr++) {
            Unit c = *itr;
            if ('0' <= c && c <= '9') {
                c -= '0';
            } else if ('a' <= c && c <= 'f') {
                c = c - 'a' + 10;

            } else if ('A' <= c && c <= 'F') {
                c = c - 'A' + 10;
            } else {
                throw;
            }
            unit |= (c << shift);
            shift = (shift + HEX_CHAR_SIZE) % UNIT_BIT_SIZE;
            if (shift == 0) {
                value.push_back(unit);
                unit = 0;
            }
        }
        value.push_back(unit);
    } else if (base == 10) {
        // Naive algorithm that make use of multiplication and addition
        *this = 0;
        Counter exp = 1;
        for (auto itr = rBegin; itr != rEnd; itr++, exp *= base) {
            Unit c = *itr;
            if ('0' <= c && c <= '9') {
                c -= '0';
            } else {
                throw;
            }
            *this += (c * exp);
        }
    } else {
        throw;
    }

    normalize();
    assert(isNormalized());
}
Example #12
0
std::string reverse_complement(const std::string& sequence) {
  auto rev = std::string{};
  rev.reserve(sequence.length());
  std::transform(sequence.crbegin(), sequence.crend(), std::back_inserter(rev), complement_base);
  return rev;
}
Example #13
0
bool EndsWith(const std::string& strData, const std::string& strSearch, bool bIgnoreCase /* = false */)
{
    return StartsWithOrEndsWith(strData.crbegin(), strData.crend(), strSearch.crbegin(), strSearch.crend(), bIgnoreCase);
}
Example #14
0
TEST(JsArgumentHelpersTest, args) {
  const bool aBool = true;
  const int64_t anInt = 17;
  const double aDouble = 3.14;
  const string aString = "word";
  const dynamic anArray = dynamic::array("a", "b", "c");
  const dynamic anObject = dynamic::object("k1", "v1")("k2", "v2");
  const string aNumericString = to<string>(anInt);

  folly::dynamic args = dynamic::array(aBool, anInt, aDouble, aString, anArray, anObject, aNumericString);

  ABI22_0_0EXPECT_EQ(jsArgAsBool(args, 0), aBool);
  ABI22_0_0EXPECT_EQ(jsArgAsInt(args, 1), anInt);
  ABI22_0_0EXPECT_EQ(jsArgAsDouble(args, 2), aDouble);
  ABI22_0_0EXPECT_EQ(jsArgAsString(args, 3), aString);
  ABI22_0_0EXPECT_EQ(jsArgAsArray(args, 4), anArray);
  ABI22_0_0EXPECT_EQ(jsArgAsObject(args, 5), anObject);

  // const args
  const folly::dynamic& cargs = args;
  const folly::dynamic& a4 = jsArgAsArray(cargs, 4);
  ABI22_0_0EXPECT_EQ(a4, anArray);
  ABI22_0_0EXPECT_EQ(jsArgAsObject(cargs, 5), anObject);

  // helpers returning dynamic should return same object without copying
  ABI22_0_0EXPECT_EQ(&jsArgAsArray(args, 4), &(args[4]));
  ABI22_0_0EXPECT_EQ(&jsArgAsArray(cargs, 4), &(args[4]));

  // dynamics returned for mutable args should be mutable.  The test is that
  // this compiles.
  jsArgAsArray(args, 4)[2] = "d";
  jsArgAsArray(args, 4)[2] = "c";
  // These fail to compile due to constness.
  // jsArgAsArray(cargs, 4)[2] = "d";
  // jsArgAsArray(cargs, 4)[2] = "c";

  // ref-qualified member function tests
  ABI22_0_0EXPECT_EQ(jsArgN(args, 3, &folly::dynamic::getString), aString);
  ABI22_0_0EXPECT_EQ(jsArg(args[3], &folly::dynamic::getString), aString);

  // conversions
  ABI22_0_0EXPECT_EQ(jsArgAsDouble(args, 1), anInt * 1.0);
  ABI22_0_0EXPECT_EQ(jsArgAsString(args, 1), aNumericString);
  ABI22_0_0EXPECT_EQ(jsArgAsInt(args, 6), anInt);

  // Test exception messages.

  // out_of_range
  ABI22_0_0EXPECT_JSAE(jsArgAsBool(args, 7),
              "JavaScript provided 7 arguments for C++ method which references at least "
              "8 arguments: out of range in dynamic array");
  // Conv range_error (invalid value conversion)
  const std::string exhead = "Could not convert argument 3 to required type: ";
  const std::string extail = ": Invalid leading character: \"word\"";
  try {
    jsArgAsInt(args, 3);
    FAIL() << "Expected JsArgumentException(" << exhead << "..." << extail << ") not thrown";
  } catch (const JsArgumentException& ex) {
    const std::string exwhat = ex.what();

    ABI22_0_0EXPECT_GT(exwhat.size(), exhead.size());
    ABI22_0_0EXPECT_GT(exwhat.size(), extail.size());

    ABI22_0_0EXPECT_TRUE(std::equal(exhead.cbegin(), exhead.cend(), exwhat.cbegin()))
      << "JsArgumentException('" << exwhat << "') does not begin with '" << exhead << "'";
    ABI22_0_0EXPECT_TRUE(std::equal(extail.crbegin(), extail.crend(), exwhat.crbegin()))
      << "JsArgumentException('" << exwhat << "') does not end with '" << extail << "'";
  }
  // inconvertible types
  ABI22_0_0EXPECT_JSAE(jsArgAsArray(args, 2),
              "Argument 3 of type double is not required type Array");
  ABI22_0_0EXPECT_JSAE(jsArgAsInt(args, 4),
              "Error converting javascript arg 4 to C++: "
              "TypeError: expected dynamic type `int/double/bool/string', but had type `array'");
  // type predicate failure
  ABI22_0_0EXPECT_JSAE(jsArgAsObject(args, 4),
              "Argument 5 of type array is not required type Object");
}
 explicit BigNumber(std::string const& num)
 {
     for(auto c = num.crbegin(); c != num.crend(); ++c)
         if(std::isdigit(*c))    push_front(*c - 48);
 }
Example #16
0
std::string path_add_separator(std::string path)
{
    if (*path.crbegin() != '/')
        path.push_back('/');
    return path;
}
Example #17
0
bool oppositeOrder(std::string input)
{
	return std::is_sorted(input.crbegin(), input.crend());
}