diff options
| author | Tor Aamodt <[email protected]> | 2010-07-15 18:09:46 -0800 |
|---|---|---|
| committer | Tor Aamodt <[email protected]> | 2010-07-15 18:09:46 -0800 |
| commit | 69f2911e04ffb1b19eef1fafb8c040af271f656e (patch) | |
| tree | 231d3b6bdc3a202f7c255bfcf7bf2c36e32cee9e /benchmarks/CUDA/CP/driver/text.py | |
creating branch for adding support for CUDA 3.x and Fermi
[git-p4: depot-paths = "//depot/gpgpu_sim_research/fermi/distribution/": change = 6829]
Diffstat (limited to 'benchmarks/CUDA/CP/driver/text.py')
| -rw-r--r-- | benchmarks/CUDA/CP/driver/text.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/benchmarks/CUDA/CP/driver/text.py b/benchmarks/CUDA/CP/driver/text.py new file mode 100644 index 0000000..b8e1097 --- /dev/null +++ b/benchmarks/CUDA/CP/driver/text.py @@ -0,0 +1,75 @@ +#! /usr/bin/env python + +from itertools import imap +import re + +TOKEN = re.compile(r"(?:^|(?<=\s))[^\s]+|(?:^|(?<!\s))[\s]+") + +COLUMNS=80 + +class iscan: + """A scan iterator.""" + def __init__(self, f, init, s): + self.f = f + self.s = s.__iter__() + self.current = init + self.begin = True + + def next(self): + if self.begin: + self.begin = False + return self.current + else: + cur = self.f(self.current, self.s.next()) # raises StopIteration + self.current = cur + return cur + + def __iter__(self): return self + +def format_columns(text, indent=0): + """Format the text to fit in 80 columns by inserting newlines. Spaces + are inserted at the beginning of each line to indent by 'indent' + columns.""" + + outls = [] + for inl in text.split('\n'): + # Split the line into alternating space and non-space tokens + tokens = TOKEN.findall(inl) + + # Empty line? + if not tokens: + outls.append("") + continue + + # Use the spaces at the beginning of the line to determine + # the line's indentation + if tokens[0].isspace(): + inl_indent = indent + len(tokens[0]) + del tokens[0] + else: + inl_indent = indent + + # Write tokens into the output + while tokens: + # Ignore whitespace at the beginning of the line + if tokens[0].isspace(): del tokens[0] + + # Take as many tokens as possible without exceeding the line + # length; however, must take at least one token + n = 0 + lengths = iscan(lambda n, t: n + len(t), inl_indent, tokens) + lengths.next() + for c in lengths: + if c > COLUMNS: break + n += 1 + n = max(n, 1) + + # Concatenate the tokens and prepend spaces for indentation + outls.append(' ' * inl_indent + "".join(tokens[:n])) + + # Continue with tokens + tokens = tokens[n:] + + return "\n".join(outls) + + |
