1 | /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | // See docs in ../ops/string_ops.cc. |
17 | |
18 | #include <string> |
19 | |
20 | #include "tensorflow/core/framework/kernel_def_builder.h" |
21 | #include "tensorflow/core/framework/op_kernel.h" |
22 | #include "tensorflow/core/framework/tensor.h" |
23 | #include "tensorflow/core/lib/core/errors.h" |
24 | #include "tensorflow/core/lib/core/status.h" |
25 | #include "tensorflow/core/lib/strings/str_util.h" |
26 | |
27 | namespace tensorflow { |
28 | |
29 | class StringStripOp : public OpKernel { |
30 | public: |
31 | explicit StringStripOp(OpKernelConstruction* context) : OpKernel(context) {} |
32 | |
33 | void Compute(OpKernelContext* ctx) override { |
34 | const Tensor* input_tensor; |
35 | OP_REQUIRES_OK(ctx, ctx->input("input" , &input_tensor)); |
36 | Tensor* output_tensor; |
37 | OP_REQUIRES_OK( |
38 | ctx, ctx->allocate_output(0, input_tensor->shape(), &output_tensor)); |
39 | |
40 | const auto input = input_tensor->flat<tstring>(); |
41 | auto output = output_tensor->flat<tstring>(); |
42 | |
43 | for (int64_t i = 0; i < input.size(); ++i) { |
44 | StringPiece entry(input(i)); |
45 | str_util::RemoveWhitespaceContext(&entry); |
46 | output(i) = string(entry); |
47 | } |
48 | } |
49 | }; |
50 | |
51 | REGISTER_KERNEL_BUILDER(Name("StringStrip" ).Device(DEVICE_CPU), StringStripOp); |
52 | |
53 | } // namespace tensorflow |
54 | |