Class: PyFormat::Field
Constant Summary collapse
- PAT =
/^ (?: (?<fill>[^}]+)? (?<align>[<>=^]) )? (?<sign>[ +-])? (?<alternate>\#)? (?<zero_padding>0)? (?<width>\d+)? (?<comma_sep>,)? (?: \.(?<precision>\d+) )? (?<type>[bcdeEfFgGnosxX%])? /x
Class Method Summary collapse
Instance Method Summary collapse
-
#format(arg) ⇒ String
format a <#Field> by @o.
-
#initialize ⇒ Field
constructor
A new instance of Field.
-
#parse_spec(spec) ⇒ nil
parse a spec string.
Constructor Details
#initialize ⇒ Field
Returns a new instance of Field.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tagen/core/string/pyformat.rb', line 137 def initialize # default options @o = { fill: " ", align: ">", sign: "-", alternate: false, zero_padding: false, width: 0, comma_sep: false, precision: nil, type: nil, } end |
Class Method Details
.parse(spec, arg) ⇒ Object
131 132 133 134 135 |
# File 'lib/tagen/core/string/pyformat.rb', line 131 def self.parse spec, arg f = self.new f.parse_spec spec if spec f.format arg end |
Instance Method Details
#format(arg) ⇒ String
format a <#Field> by @o
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/tagen/core/string/pyformat.rb', line 196 def format arg # arg is int str .. # ret is str. case arg when Integer @o[:type] ||= 'd' else # default is s @o[:type] ||= 's' end ret = case @o[:type] when 's' arg = arg.to_s @o[:precision] ? arg[0...@o[:precision]] : arg when 'c' arg.chr when 'b','o','d','x','X' arg = arg.to_i case @o[:type] when 'b' ret1 = arg.to_s 2 ret1 = do_comma ret1 @o[:alternate] ? '0b'+ret1 : ret1 when 'o' ret1 = arg.to_s 8 ret1 = do_comma ret1 @o[:alternate] ? '0'+ret1 : ret1 when 'd' ret1 = arg.to_s 10 do_comma ret1 when 'x', 'X' ret1 = arg.to_s 16 ret1.upcase! if @o[:type]=='X' ret1 = do_comma ret1 @o[:alternate] ? "0#{@o[:type]}"+ret1 : ret1 end # for float, need handle 'precision' when 'f','F','g','G','e','E', '%' type = @o[:type] num = arg.to_f if type=='%' num = num*100 type = 'g' elsif type=='F' type = 'f' end # remove 0 1.00000 if type=='f' sa, sb = num.to_s.split('.') prec = sb.length @o[:precision] = prec if not @o[:precision] elsif type=='e' # not implement yet end spec = "%" spec += '.' + @o[:precision].to_s if @o[:precision] spec += type ret1 = spec % num # '%g' % 1.0 => 1 # 'comma_sep' if not %w(g G).include? type a, b = ret1.split('.') a = do_comma a ret1 = b==nil ? a : a+'.'+b end ret1 += '%' if @o[:type]=='%' ret1 end # case ## sign if @o[:sign] != '-' sign = arg.to_f>=0 ? @o[:sign] : '-' ret = sign+ret end ## width n = @o[:width] - ret.length if n > 0 fill = '' @o[:fill].chars.cycle do |c| fill << c break if fill.length == n end ret = case @o[:align] when '>' then fill + ret when '<' then ret + fill when '^' then fill[0...fill.length/2] + ret + fill[fill.length/2..-1] when '=' then ret[0] + fill + ret[1..-1] end end ret end |
#parse_spec(spec) ⇒ nil
parse a spec string
parse_spec “this is PyFormat::Field.::.2f” return @o
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/tagen/core/string/pyformat.rb', line 159 def parse_spec spec matched = PAT.match(spec) raise EFormatSpec, spec if not matched matched = matched.to_hash # merge @o and matched @o.each do |k,v| @o[k] = matched[k] ? matched[k] : v end # handle keys @o = @o.each.with_object({}) do |(k,v),o| case k when :width, :precision o[k] = v.to_i if v when :zero_padding if v o[:fill] = "0" end when :precision o[:precision] = 1 if v<1 else o[k] = v end end if @o[:align] == "=" @o[:fill] = "0" @o[:sign] = "+" end end |