1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "components/segmentation_platform/public/constants.h"
#include <cstring>
#include "base/strings/string_util.h"
#include "components/segmentation_platform/public/proto/segmentation_platform.pb.h"
namespace segmentation_platform {
namespace {
std::string SegmentIdToHistogramVariantInternal(
const proto::SegmentId segment_id) {
std::string name = proto::SegmentId_Name(segment_id);
// Remove the project name prefixes from the segment IDs and change to lower
// case.
constexpr char kOptimizationSegmentationPrefix[] =
"OPTIMIZATION_TARGET_SEGMENTATION_";
constexpr char kOptimizationTargetPrefix[] = "OPTIMIZATION_TARGET_";
if (base::StartsWith(name, kOptimizationSegmentationPrefix)) {
name.erase(0, strlen(kOptimizationSegmentationPrefix));
} else if (base::StartsWith(name, kOptimizationTargetPrefix)) {
name.erase(0, strlen(kOptimizationTargetPrefix));
}
name = base::ToLowerASCII(name);
// Convert to camel case.
std::string result;
bool cap_first_letter = true;
for (char ch : name) {
if ('a' <= ch && ch <= 'z') {
if (cap_first_letter) {
constexpr char toupper = ('A' - 'a');
result += ch + toupper;
} else {
result += ch;
}
cap_first_letter = false;
} else if ('0' <= ch && ch <= '9') {
result += ch;
cap_first_letter = true;
} else {
CHECK_EQ(ch, '_');
cap_first_letter = true;
}
}
return result;
}
} // namespace
// Please keep in sync with SegmentationModel variant in
// //tools/metrics/histograms/metadata/segmentation_platform/histograms.xml.
// Should also update the field trials allowlist in
// go/segmentation-field-trials-map.
std::string SegmentIdToHistogramVariant(proto::SegmentId segment_id) {
// This case is reached when UNKNOWN segment is valid, in case of boolean
// segment results.
if (segment_id == proto::SegmentId::OPTIMIZATION_TARGET_UNKNOWN) {
return "Other";
}
return SegmentIdToHistogramVariantInternal(segment_id);
}
std::string GetSubsegmentKey(const std::string& segmentation_key) {
return segmentation_key + kSubsegmentDiscreteMappingSuffix;
}
} // namespace segmentation_platform
|