LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
StreamHelper.h
Go to the documentation of this file.
1//===-- StreamHelper.h ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
12#include <llvm/Support/raw_ostream.h>
13
14namespace llzk {
15
17class filtered_raw_ostream : public llvm::raw_ostream {
18 llvm::raw_ostream &underlyingStream;
19 std::function<bool(char)> filterFunc;
20
21 void write_impl(const char *ptr, size_t size) override {
22 for (size_t i = 0; i < size; ++i) {
23 if (!filterFunc(ptr[i])) {
24 underlyingStream << ptr[i];
25 }
26 }
27 }
28
29 uint64_t current_pos() const override { return underlyingStream.tell(); }
30
31public:
32 filtered_raw_ostream(llvm::raw_ostream &os, std::function<bool(char)> filter)
33 : underlyingStream(os), filterFunc(std::move(filter)) {}
34
35 ~filtered_raw_ostream() override { flush(); }
36};
37
40template <typename Func, typename... Args>
41inline std::string buildStringViaCallback(Func &&appendFn, Args &&...args) {
42 std::string output;
43 llvm::raw_string_ostream oss(output);
44 std::invoke(std::forward<Func>(appendFn), oss, std::forward<Args>(args)...);
45 return output;
46}
47
50template <typename T> inline std::string buildStringViaPrint(const T &base) {
51 return buildStringViaCallback([](llvm::raw_ostream &ss, const T &b) { b.print(ss); }, base);
52}
53
56template <typename... Args> inline std::string buildStringViaInsertionOp(Args &&...args) {
57 std::string output;
58 llvm::raw_string_ostream oss(output);
59 (oss << ... << std::forward<Args>(args));
60 return output;
61}
62
63} // namespace llzk
filtered_raw_ostream(llvm::raw_ostream &os, std::function< bool(char)> filter)
std::string buildStringViaInsertionOp(Args &&...args)
Generate a string by using the insertion operator (<<) to append all args to a stream backed by the r...
std::string buildStringViaPrint(const T &base)
Generate a string by calling base.print(llvm::raw_ostream &) on a stream backed by the returned strin...
std::string buildStringViaCallback(Func &&appendFn, Args &&...args)
Generate a string by calling the given appendFn with an llvm::raw_ostream & as the first argument fol...