def replace_func(tx, symbol, pos, match_num): if char_at(tx, pos + 1) == '{' or char_at(tx, pos + 1).isdigit(): # it's a variable, skip it return False # replace sequence of $ symbols with padded number j = pos + 1 if j < len(text): while char_at(tx, j) == '$' and char_at(tx, j + 1) != '{': j += 1 return (tx[pos:j], value.zfill(j - pos))
def has_output_placeholder(text): """ Test if text contains output placeholder $# @param text: str @return: bool """ for i, ch in enumerate(text): if ch == '\\': # escaped char continue; elif ch == '$' and char_at(text, i + 1) == '#': return True return False
def has_output_placeholder(text): """ Test if text contains output placeholder $# @param text: str @return: bool """ for i, ch in enumerate(text): if ch == '\\': # escaped char continue elif ch == '$' and char_at(text, i + 1) == '#': return True return False
def next_while(ix, fn): while ix < il: if not fn(char_at(text, ix)): break ix += 1 return ix
def process_text_before_paste(text, escape_fn, tabstop_fn): """ Process text that should be pasted into editor: clear escaped text and handle tabstops @type text: str @param escape_fn: Handle escaped character. Must return replaced value @type escape_fn: function @param tabstop_fn: Callback function that will be called on every tabstob occurance, passing <b>index</b>, <code>number</code> and <b>value</b> (if exists) arguments. This function must return replacement value @type tabstop_fn: function @returns: str """ i = 0 il = len(text) str_builder = [] def next_while(ix, fn): while ix < il: if not fn(char_at(text, ix)): break ix += 1 return ix while i < il: ch = text[i] if ch == '\\' and i + 1 < il: # handle escaped character str_builder.append(escape_fn(text[i + 1])) i += 2 continue elif ch == '$': # looks like a tabstop next_ch = char_at(text, i + 1) _i = i if next_ch.isdigit(): # $N placeholder start_ix = i + 1 i = next_while(start_ix, lambda n: n.isdigit()) if start_ix < i: str_builder.append(tabstop_fn(_i, text[start_ix:i])) continue elif next_ch == '{': # ${N:value} or ${N} placeholder brace_count = [1] start_ix = i + 2 i = next_while(start_ix, lambda n: n.isdigit()) if i > start_ix: if char_at(text, i) == '}': str_builder.append(tabstop_fn(_i, text[start_ix:i])) i += 1 # handle closing brace continue elif char_at(text, i) == ':': val_start = i + 2 def fn(c): if c == '{': brace_count[0] += 1 elif c == '}': brace_count[0] -= 1 return bool(brace_count[0]) i = next_while(val_start, fn) str_builder.append(tabstop_fn(_i, text[start_ix:val_start - 2], text[val_start - 1:i])) i += 1 # handle closing brace continue i = _i # push current character to stack str_builder.append(ch) i += 1 return ''.join(str_builder)
def next_byte(): char = char_at(stream, pos[0]) pos[0] += 1 return ord(char)
def process_text_before_paste(text, escape_fn, tabstop_fn): """ Process text that should be pasted into editor: clear escaped text and handle tabstops @type text: str @param escape_fn: Handle escaped character. Must return replaced value @type escape_fn: function @param tabstop_fn: Callback function that will be called on every tabstob occurance, passing <b>index</b>, <code>number</code> and <b>value</b> (if exists) arguments. This function must return replacement value @type tabstop_fn: function @returns: str """ i = 0 il = len(text) str_builder = [] def next_while(ix, fn): while ix < il: if not fn(char_at(text, ix)): break ix += 1 return ix while i < il: ch = text[i] if ch == '\\' and i + 1 < il: # handle escaped character str_builder.append(escape_fn(text[i + 1])) i += 2 continue elif ch == '$': # looks like a tabstop next_ch = char_at(text, i + 1) _i = i if next_ch.isdigit(): # $N placeholder start_ix = i + 1 i = next_while(start_ix, lambda n: n.isdigit()) if start_ix < i: str_builder.append(tabstop_fn(_i, text[start_ix:i])) continue elif next_ch == '{': # ${N:value} or ${N} placeholder brace_count = [1] start_ix = i + 2 i = next_while(start_ix, lambda n: n.isdigit()) if i > start_ix: if char_at(text, i) == '}': str_builder.append(tabstop_fn(_i, text[start_ix:i])) i += 1 # handle closing brace continue elif char_at(text, i) == ':': val_start = i + 2 def fn(c): if c == '{': brace_count[0] += 1 elif c == '}': brace_count[0] -= 1 return bool(brace_count[0]) i = next_while(val_start, fn) str_builder.append( tabstop_fn(_i, text[start_ix:val_start - 2], text[val_start - 1:i])) i += 1 # handle closing brace continue i = _i # push current character to stack str_builder.append(ch) i += 1 return ''.join(str_builder)