Execute the specified regex using the given context. Context requirements: - "input" string Tensor at input_index=0 - "output" string Tensor at output_index=0
| 30 | // - "input" string Tensor at input_index=0 |
| 31 | // - "output" string Tensor at output_index=0 |
| 32 | Status InternalCompute(const RE2& match, const string& rewrite, |
| 33 | const bool replace_global, OpKernelContext* ctx) { |
| 34 | const Tensor* input_tensor; |
| 35 | TF_RETURN_IF_ERROR(ctx->input("input", &input_tensor)); |
| 36 | Tensor* output_tensor; |
| 37 | std::unique_ptr<Tensor> maybe_forwarded = |
| 38 | ctx->forward_input(0 /*input_index*/, 0 /*output_index*/, |
| 39 | tensorflow::DT_STRING, input_tensor->shape(), |
| 40 | ctx->input_memory_type(0), ctx->input_alloc_attr(0)); |
| 41 | if (maybe_forwarded) { |
| 42 | output_tensor = maybe_forwarded.get(); |
| 43 | TF_RETURN_IF_ERROR(ctx->set_output("output", *output_tensor)); |
| 44 | } else { |
| 45 | TF_RETURN_IF_ERROR( |
| 46 | ctx->allocate_output("output", input_tensor->shape(), &output_tensor)); |
| 47 | output_tensor->flat<tstring>() = input_tensor->flat<tstring>(); |
| 48 | } |
| 49 | auto output_flat = output_tensor->flat<tstring>(); |
| 50 | for (size_t i = 0; i < output_flat.size(); ++i) { |
| 51 | if (replace_global) { |
| 52 | RE2::GlobalReplace(&output_flat(i), match, rewrite); |
| 53 | } else { |
| 54 | RE2::Replace(&output_flat(i), match, rewrite); |
| 55 | } |
| 56 | } |
| 57 | return Status::OK(); |
| 58 | } |
| 59 | } // namespace |
| 60 | |
| 61 | class RegexReplaceOp : public OpKernel { |
no test coverage detected