56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
"""三方接收同一 argv,重复读取独立参数列表并累计全部字符。"""
|
|
|
|
|
|
def source(language):
|
|
if language == 'dever':
|
|
return '''loop characters(values: List<Text>) (out: Int)
|
|
| each value in values | result |
|
|
| out | sum length(value) |
|
|
flow read() (out: Int)
|
|
| step | kind | do |
|
|
| values | flow | dever.process.arguments() |
|
|
| out | loop | characters(values) |
|
|
loop measure(n: Int) (out: Int)
|
|
| each value in range(0, n) | result |
|
|
| out | sum read() |
|
|
cmd bench = measure
|
|
'''
|
|
if language in ('rust', 'runtime'):
|
|
arguments = ('dever_runtime::io::arguments().unwrap()' if language == 'runtime' else
|
|
'std::env::args_os().skip(1).map(|value| value.into_string()).collect::<Result<Vec<_>, _>>().unwrap()')
|
|
return '''fn read() -> i64 {
|
|
let values = ARGUMENTS;
|
|
let mut count = 0i64;
|
|
for value in &values { count = count.checked_add(i64::try_from(value.chars().count()).unwrap()).unwrap(); }
|
|
count
|
|
}
|
|
fn main() {
|
|
let input: serde_json::Value = serde_json::from_str(&std::env::args().nth(2).unwrap()).unwrap();
|
|
let n = input["n"].as_i64().unwrap();
|
|
let mut count = 0i64;
|
|
for _ in 0..n {
|
|
count = count.checked_add(read()).unwrap();
|
|
}
|
|
println!("{count}");
|
|
}
|
|
'''.replace('ARGUMENTS', arguments)
|
|
return '''package main
|
|
import ("encoding/json"; "fmt"; "os"; "unicode/utf8")
|
|
func main() {
|
|
var input struct { N int64 }
|
|
if err := json.Unmarshal([]byte(os.Args[2]), &input); err != nil { panic(err) }
|
|
count := int64(0)
|
|
for i := int64(0); i < input.N; i++ {
|
|
values := append([]string(nil), os.Args[1:]...)
|
|
subtotal := int64(0)
|
|
for _, value := range values {
|
|
if !utf8.ValidString(value) { panic("process argument is not UTF-8") }
|
|
size := int64(utf8.RuneCountInString(value))
|
|
next := subtotal + size; if next < subtotal { panic("count overflow") }; subtotal = next
|
|
}
|
|
next := count + subtotal; if next < count { panic("count overflow") }; count = next
|
|
}
|
|
fmt.Println(count)
|
|
}
|
|
'''
|