コード例 #1
0
ファイル: regexp.cpp プロジェクト: letmefly/edbee-lib
    /// Constructs the OnigRegExp engine
    /// @param pattern the regular expression pattern
    /// @param caseSensitive is the regexp case senstitive
    /// @param syntax the syntax to use
    OnigRegExpEngine( const QString& pattern, bool caseSensitive, RegExp::Syntax syntax )
        : reg_(0)
        , region_(0)
        , pattern_(pattern)
    {
        const QChar* patternChars = pattern.constData();

        OnigSyntaxType* onigSyntax = ONIG_SYNTAX_DEFAULT;
        if( syntax == RegExp::SyntaxFixedString ) { onigSyntax = ONIG_SYNTAX_ASIS; }


        OnigOptionType onigOptions = ONIG_OPTION_NONE|ONIG_OPTION_CAPTURE_GROUP;
        if( !caseSensitive ) { onigOptions = onigOptions | ONIG_OPTION_IGNORECASE;}

        int result = onig_new(&reg_, (OnigUChar*)patternChars, (OnigUChar*)(patternChars + pattern.length()), onigOptions, ONIG_ENCODING_UTF16_LE, ONIG_SYNTAX_DEFAULT, &einfo_);
        valid_ = result == ONIG_NORMAL;
        fillError( result );
    }
コード例 #2
0
ファイル: myUtil.cpp プロジェクト: Super169/MyLib
//
// uint8_t f2s(char* buffer, double value, uint8_t decimal, uint8_t width, uint8_t padMode, char padChar) 
//
// Convert the value into string with give format, and return the length of string
// - decimal: decimal place
// - width: size of the string (the buffer must have size not less than width + 1 )
// - padMode: 0 - fit value '1.0', 1 - Right align '  1.0', 2 - Left align  '1.0  '
// - padChar: character used to fill the empty space, will be ignored if padMode = 0        
//
//
uint8_t myUtil::f2s(char* buffer, double value, uint8_t decimal, uint8_t width, uint8_t padMode, char padChar) {

  if (width == 0) return false;

  double absValue, adjValue;
  // for safety, don't use unsigned int here, as the length (e.g. len_rem) can be negative after calculation
  int len_ne, len_int, len_dec, len_rem, data_len;

  if (value < 0) {
    len_ne = 1;
    absValue = - value;
  } else {
    len_ne = 0;
    absValue = value;
  }
  len_int = getLen(absValue);
  if (len_ne + len_int > width) {
    return fillError(buffer, width);
  }

  len_dec = (decimal > 0 ? 1 : 0);
  if (decimal) {
    len_rem = width - (len_ne + len_int + len_dec);
    if (len_rem > decimal) {
      len_rem = decimal;
    } else {
      if (len_rem <= 0) {
        len_rem = 0;
        len_dec = 0;  // no need to display decimal place
      }
    }
  } else {
    len_rem = 0;
  }

  data_len = len_ne + len_int + len_dec + len_rem;
  adjValue = absValue + 0.5 / getFactor(len_rem);

  // check if addition digits after round up (e.g. 999.5 -> 1000.0)
  if (getLen(adjValue) > len_int) {
    len_int++;
    if (len_ne + len_int > width) {
      return fillError(buffer, width);
    }
    if ((width > 0) && (data_len >= width)) {
      // try to reduce decimal to fit the display
      if (len_rem > 0) {
        len_rem--;
        adjValue = absValue + 0.5 / getFactor(len_rem);
        if (len_rem == 0) len_dec = 0;
      }
    }
    data_len = len_ne + len_int + len_dec + len_rem;
  }
  
  // Fill buffer with 0 as terminator anywhere, * size = width + 1
  for (int idx = 0; idx <= width; idx++) buffer[idx] = 0;

  if (len_rem == 0) len_dec = 0;
  data_len = len_ne + len_int + len_dec + len_rem;

  uint8_t ptr = 0;
  // pad left
  if (padMode == _F2S_PAD_LEFT) while (ptr < width - data_len) buffer[ptr++] = padChar;

  if (len_ne) buffer[ptr++] = '-';

  unsigned long adjInt = (unsigned long) adjValue;

  setNum(buffer, ptr, adjInt, len_int);
  ptr += len_int;
  if (len_rem > 0) {
    buffer[ptr++] = '.';
    unsigned long adjRem = getFactor(len_rem) * (adjValue - adjInt);
    setNum(buffer, ptr, adjRem, len_rem);
    ptr += len_rem;
  }
  // pad right
  if (padMode == _F2S_PAD_RIGHT) while (ptr < width) buffer[ptr++] = padChar;

  // Just for safety, should already filled with 0 before
  buffer[ptr] = 0;
  return ptr;
  
}