From: Dietmar Maurer Date: Fri, 28 Oct 2016 04:56:55 +0000 (+0200) Subject: CpuSet: add helper to format/print cpu sets. X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=284dca701b3461cd41efe52c1c395a9a7a42095b;hp=2862235726cd0252e092460cf50e6e56906be31f CpuSet: add helper to format/print cpu sets. --- 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;