tensorflow/core/kernels/regex_replace_op.cc
// Execute the specified regex using the given context.
// Context requirements:
//  - "input" string Tensor at input_index=0
//  - "output" string Tensor at output_index=0
Execute the specified regex using the given context.
Context requirements:
"input" string Tensor at input_index=0
"output" string Tensor at output_index=0
Execute the specified regex using the given context . Context requirements : " input " string Tensor at input_index = 0 " output " string Tensor at output_index = 0
Status InternalCompute(const RE2& regex, const string& rewrite,
                       const bool replace_global, OpKernelContext* ctx) {
  const Tensor* input_tensor;
  TF_RETURN_IF_ERROR(ctx->input("input", &input_tensor));
  Tensor* output_tensor;
  std::unique_ptr<Tensor> maybe_forwarded =
      ctx->forward_input(0 , 0 ,
                         tensorflow::DT_STRING, input_tensor->shape(),
                         ctx->input_memory_type(0), ctx->input_alloc_attr(0));
  if (maybe_forwarded) {
    output_tensor = maybe_forwarded.get();
    TF_RETURN_IF_ERROR(ctx->set_output("output", *output_tensor));
  } else {
    TF_RETURN_IF_ERROR(
        ctx->allocate_output("output", input_tensor->shape(), &output_tensor));
    output_tensor->flat<tstring>() = input_tensor->flat<tstring>();
  }
  auto output_flat = output_tensor->flat<tstring>();
  for (size_t i = 0; i < output_flat.size(); ++i) {
    string buf = output_flat(i);
    if (replace_global) {
      RE2::GlobalReplace(&buf, regex, rewrite);
    } else {
      RE2::Replace(&buf, regex, rewrite);
    }
    output_flat(i) = std::move(buf);
  }
  return Status::OK();
}
Status InternalCompute ( const RE2 & regex , const string & rewrite , const bool replace_global , OpKernelContext * ctx ) { const Tensor * input_tensor ; TF_RETURN_IF_ERROR ( ctx -> input ( " " , & input_tensor ) ) ; Tensor * output_tensor ; std :: unique_ptr < Tensor > maybe_forwarded = ctx -> forward_input ( 0 , 0 , tensorflow :: DT_STRING , input_tensor -> shape ( ) , ctx -> input_memory_type ( 0 ) , ctx -> input_alloc_attr ( 0 ) ) ; if ( maybe_forwarded ) { output_tensor = maybe_forwarded . get ( ) ; TF_RETURN_IF_ERROR ( ctx -> set_output ( " " , * output_tensor ) ) ; } else { TF_RETURN_IF_ERROR ( ctx -> allocate_output ( " " , input_tensor -> shape ( ) , & output_tensor ) ) ; output_tensor -> flat < tstring > ( ) = input_tensor -> flat < tstring > ( ) ; } auto output_flat = output_tensor -> flat < tstring > ( ) ; for ( size_t i = 0 ; i < output_flat . size ( ) ; ++ i ) { string buf = output_flat ( i ) ; if ( replace_global ) { RE2 :: GlobalReplace ( & buf , regex , rewrite ) ; } else { RE2 :: Replace ( & buf , regex , rewrite ) ; } output_flat ( i ) = std :: move ( buf ) ; } return Status :: OK ( ) ; }
Execute the specified regex using the given context.
Execute the specified regex using the given context .
/*input_index*/ /*output_index*/ // TODO(dero): Mitigate copy; Global and GlobalReplace below currently only // accept std::string.