如何重现BOM转换错误

时间:2017-12-09 20:47:45

标签: ruby character-encoding utf

我有一个Web应用程序正在接收抛出错误的POST数据:

Encoding::UndefinedConversionError: "\xEF" from ASCII-8BIT to UTF-8

我假设这是由数据中的BOM引起的。

我无法在测试中重现错误。有没有办法制作一个字符串来导致此错误?

1 个答案:

答案 0 :(得分:0)

一个独立的程序来重现错误:

var gulp       = require('gulp'),
    gutil      = require('gulp-util'),
    jshint     = require('gulp-jshint'),
    uglify     = require('gulp-uglify'),
    sass       = require('gulp-sass'),
    concat     = require('gulp-concat'),
    sourcemaps = require('gulp-sourcemaps'),
    pump       = require('pump'),
    notify     = require('gulp-notify'),
    plumber    = require('gulp-plumber'),
    growl      = require('growl'),

   input  = {
      'sass': 'scss/**/*.scss',
      'javascript': 'js/**/*.js'
   },

   output = {
      'stylesheets': 'dist/styles',
      'javascript': 'dist/javascript'
   };

gulp.task('build-js', function() {
  return gulp.src(input.javascript)
    .pipe(sourcemaps.init())
      .pipe(concat('test.js'))
      //only uglify if gulp is ran with '--type production'
      .pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(output.javascript));
});

gulp.task('build-css', function() {
  return gulp.src('scss/**/*.scss')
    .pipe(sourcemaps.init())
      .pipe(sass())
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(output.stylesheets));
});

gulp.task('watch', function() {
  notify("Watching for changes...").write('');
  gulp.watch(input.javascript, ['jshint', 'build-js']);
  gulp.watch(input.sass, ['build-css']);
  .pipe(notify({
      message: "Generated file: <%= file.relative %> @ <%= options.date %>",
      templateOptions: {
        date: new Date()
      }
    }))
});

如果你需要在程序中使用它,你可以使用`force_encoding来获得类似的结果:

#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("\t "));

结果:

#encoding: ascii
string = "\xEFTest"

puts string.encoding
puts string.encode('utf-8')