X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FMisc.pm;h=f7a8bcedde8b19a0ba4b730191f018a345799748;hb=64419f5890a16922f5692dc263bb43fba3e4b22d;hp=2559fc3327675776e38505567807ed613d2e410f;hpb=c7f11e631d32a61a4e4d02daee64e06ea77318d3;p=freeside.git

diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm
index 2559fc332..f7a8bcedd 100644
--- a/FS/FS/Misc.pm
+++ b/FS/FS/Misc.pm
@@ -22,6 +22,7 @@ use Encode;
                  generate_ps generate_pdf do_print
                  csv_from_fixed
                  ocr_image
+                 bytes_substr
                );
 
 $DEBUG = 0;
@@ -958,6 +959,26 @@ sub ocr_image {
   @lines;
 }
 
+=item bytes_substr STRING, OFFSET[, LENGTH[, REPLACEMENT] ]
+
+A replacement for "substr" that counts raw bytes rather than logical 
+characters. Unlike "bytes::substr", will suppress fragmented UTF-8 characters
+rather than output them. Unlike real "substr", is not an lvalue.
+
+=cut
+
+sub bytes_substr {
+  my ($string, $offset, $length, $repl) = @_;
+  my $bytes = substr(
+    Encode::encode('utf8', $string),
+    $offset,
+    $length,
+    Encode::encode('utf8', $repl)
+  );
+  my $chk = $DEBUG ? Encode::FB_WARN : Encode::FB_QUIET;
+  return Encode::decode('utf8', $bytes, $chk);
+}
+
 =back
 
 =head1 BUGS