Example #1
0
  Fixnum* String::tr_expand(STATE, Object* limit) {
    struct tr_data tr_data;
    native_int seq;
    native_int max;
    native_int chr;

    tr_data.last = 0;
    tr_data.steps = 0;

    if(Integer* lim = try_as<Integer>(limit)) {
      tr_data.limit = lim->to_native();
    } else {
      tr_data.limit = -1;
    }

    unsigned char* str = data_->bytes;
    native_int bytes = (native_int)this->size();
    native_int start = bytes > 1 && str[0] == '^' ? 1 : 0;
    std::memset(tr_data.set, -1, sizeof(native_int) * 256);

    for(native_int i = start; i < bytes;) {
      chr = str[i];
      seq = ++i < bytes ? str[i] : -1;

      if(seq == '-') {
        max = ++i < bytes ? str[i] : -1;
        if(max >= 0) {
          while(chr <= max) {
            if(tr_data.assign(chr)) return tr_replace(state, &tr_data);
            chr++;
          }
          i++;
        } else {
          if(tr_data.assign(chr)) return tr_replace(state, &tr_data);
          if(tr_data.assign(seq)) return tr_replace(state, &tr_data);
        }
      } else if(chr == '\\' && seq >= 0) {
        continue;
      } else {
        if(tr_data.assign(chr)) return tr_replace(state, &tr_data);
      }
    }

    return tr_replace(state, &tr_data);
  }
Example #2
0
Fixnum* String::tr_expand(STATE, Object* limit, Object* invalid_as_empty) {
    struct tr_data tr_data;

    tr_data.last = 0;
    tr_data.steps = 0;

    if(Fixnum* lim = try_as<Fixnum>(limit)) {
        tr_data.limit = lim->to_native();
    } else {
        tr_data.limit = -1;
    }

    uint8_t* str = byte_address();
    native_int bytes = (native_int)size();
    native_int start = bytes > 1 && str[0] == '^' ? 1 : 0;
    memset(tr_data.set, -1, sizeof(native_int) * 256);

    for(native_int i = start; i < bytes;) {
        native_int chr = str[i];
        native_int seq = ++i < bytes ? str[i] : -1;

        if(chr == '\\' && seq >= 0) {
            continue;
        } else if(seq == '-') {
            native_int max = ++i < bytes ? str[i] : -1;
            if(max >= 0 && chr > max && RTEST(invalid_as_empty)) {
                i++;
            } else if(max >= 0) {
                do {
                    if(tr_data.assign(chr)) return tr_replace(state, &tr_data);
                    chr++;
                } while(chr <= max);
                i++;
            } else {
                if(tr_data.assign(chr)) return tr_replace(state, &tr_data);
                if(tr_data.assign(seq)) return tr_replace(state, &tr_data);
            }
        } else {
            if(tr_data.assign(chr)) return tr_replace(state, &tr_data);
        }
    }

    return tr_replace(state, &tr_data);
}
Example #3
0
void ReplaceDlg::replace_directed(HWindow *pDlg, int finddir, bool showCount)
{
    bool case_sensitive = pDlg->IsDlgButtonChecked(IDC_MATCHCASE_CHECK) == BST_CHECKED;
    GetDlgItemText(pDlg, IDC_TO_REPLACE_EDIT, strToReplaceData);
    GetDlgItemText(pDlg, IDC_REPLACEWITH_EDIT, strReplaceWithData);
    bPasteAsText = pDlg->IsDlgButtonChecked(IDC_USETRANSLATION_CHECK) == BST_UNCHECKED;
    //------------------
    // Don't do anything if to-replace and replace-with data are same.
    Text2BinTranslator tr_find(strToReplaceData), tr_replace(strReplaceWithData);
    if (tr_find.bCompareBin(tr_replace, iCharacterSet, iBinaryMode))
    {
        MessageBox(pDlg, GetLangString(IDS_REPL_SAME_STRS), MB_ICONERROR);
        return;
    }
    WaitCursor wc;
    int occ_num = 0;
    HWindow *pwndFocus = HWindow::GetFocus();
    if (EnableDlgItem(pDlg, IDC_REPLACE_BUTTON, FALSE) == FALSE)
    {
        // Don't lose focus.
        if (!pwndFocus->IsWindowEnabled())
            pDlg->SetFocus();
        occ_num++;
        replace_selected_data(pDlg);
    }
    if (finddir)
    {
        while (find_and_select_data(finddir, case_sensitive))
        {
            occ_num++;
            replace_selected_data(pDlg);
        };
    }
    if (occ_num)
    {
        set_wnd_title();
        adjust_view_for_selection();
        resize_window();
        synch_sibling();

        if (showCount)
        {
            TCHAR tbuf[100];
            _stprintf(tbuf, GetLangString(IDS_REPL_COUNT), occ_num);
            MessageBox(pDlg, tbuf, MB_ICONINFORMATION);
        }
    }
}