From 284dca701b3461cd41efe52c1c395a9a7a42095b Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 28 Oct 2016 06:56:55 +0200 Subject: [PATCH] CpuSet: add helper to format/print cpu sets. --- src/PVE/CpuSet.pm | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/PVE/CpuSet.pm b/src/PVE/CpuSet.pm index ef660b5..4004bb3 100644 --- a/src/PVE/CpuSet.pm +++ b/src/PVE/CpuSet.pm @@ -136,4 +136,35 @@ sub is_equal { return 1; } +sub short_string { + my ($self) = @_; + + my @members = $self->members(); + + my $res = ''; + my ($last, $next); + foreach my $cpu (@members) { + if (!defined($last)) { + $last = $next = $cpu; + } elsif (($next + 1) == $cpu) { + $next = $cpu; + } else { + $res .= ',' if length($res); + $res .= "$last-$next"; + $last = $next = undef; + } + } + + if (defined($last)) { + $res .= ',' if length($res); + if ($last != $next) { + $res .= "$last-$next"; + } else { + $res .= "$last"; + } + } + + return $res; +} + 1; -- 2.39.2