utils.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "utils.h"
  17. namespace android {
  18. namespace lshal {
  19. std::string toHexString(uint64_t t) {
  20. std::ostringstream os;
  21. os << std::hex << std::setfill('0') << std::setw(16) << t;
  22. return os.str();
  23. }
  24. std::vector<std::string> split(const std::string &s, char c) {
  25. std::vector<std::string> components{};
  26. size_t startPos = 0;
  27. size_t matchPos;
  28. while ((matchPos = s.find(c, startPos)) != std::string::npos) {
  29. components.push_back(s.substr(startPos, matchPos - startPos));
  30. startPos = matchPos + 1;
  31. }
  32. if (startPos <= s.length()) {
  33. components.push_back(s.substr(startPos));
  34. }
  35. return components;
  36. }
  37. void replaceAll(std::string *s, char from, char to) {
  38. for (size_t i = 0; i < s->size(); ++i) {
  39. if (s->at(i) == from) {
  40. s->at(i) = to;
  41. }
  42. }
  43. }
  44. } // namespace lshal
  45. } // namespace android