# Global Metrics

path: .metrics.nargs.sum
old: 32.0
new: 14.0
path: .metrics.nargs.average
old: 0.8
new: 3.5
path: .metrics.loc.sloc
old: 305.0
new: 197.0
path: .metrics.loc.lloc
old: 58.0
new: 56.0
path: .metrics.loc.blank
old: 52.0
new: 15.0
path: .metrics.loc.ploc
old: 173.0
new: 125.0
path: .metrics.loc.cloc
old: 80.0
new: 57.0
path: .metrics.cyclomatic.sum
old: 52.0
new: 33.0
path: .metrics.cyclomatic.average
old: 1.181818181818182
new: 6.6
path: .metrics.cognitive.average
old: 0.225
new: 11.0
path: .metrics.cognitive.sum
old: 9.0
new: 44.0
path: .metrics.mi.mi_visual_studio
old: 12.449042448978629
new: 19.74678805288107
path: .metrics.mi.mi_sei
old: -4.056669205546271
new: 13.382971507890415
path: .metrics.mi.mi_original
old: 21.28786258775345
new: 33.76700757042663
path: .metrics.nexits.average
old: 0.775
new: 1.75
path: .metrics.nexits.sum
old: 31.0
new: 7.0
path: .metrics.nom.functions
old: 40.0
new: 4.0
path: .metrics.nom.total
old: 40.0
new: 4.0
path: .metrics.halstead.difficulty
old: 94.91525423728814
new: 40.09146341463415
path: .metrics.halstead.estimated_program_length
old: 507.07593991234864
new: 637.415669123051
path: .metrics.halstead.N1
old: 545.0
new: 446.0
path: .metrics.halstead.length
old: 895.0
new: 709.0
path: .metrics.halstead.n1
old: 32.0
new: 25.0
path: .metrics.halstead.bugs
old: 2.245298083507359
new: 1.1079337264455416
path: .metrics.halstead.time
old: 30712.868866926427
new: 10645.842856989017
path: .metrics.halstead.vocabulary
old: 91.0
new: 107.0
path: .metrics.halstead.n2
old: 59.0
new: 82.0
path: .metrics.halstead.N2
old: 350.0
new: 263.0
path: .metrics.halstead.level
old: 0.010535714285714284
new: 0.02494296577946768
path: .metrics.halstead.purity_ratio
old: 0.566565295991451
new: 0.8990347942497193
path: .metrics.halstead.volume
old: 5824.476202977833
new: 4779.700093358413
path: .metrics.halstead.effort
old: 552831.6396046757
new: 191625.17142580223

# Spaces Data

## Minimal test - lines (119, 146)

path: .spaces[2].metrics.halstead.N1
old: 15.0
new: 85.0
path: .spaces[2].metrics.halstead.n1
old: 9.0
new: 19.0
path: .spaces[2].metrics.halstead.purity_ratio
old: 1.76156400069231
new: 1.436547645983914
path: .spaces[2].metrics.halstead.N2
old: 10.0
new: 52.0
path: .spaces[2].metrics.halstead.level
old: 0.13333333333333333
new: 0.05060728744939271
path: .spaces[2].metrics.halstead.bugs
old: 0.027087385792977793
new: 0.20074683367455948
path: .spaces[2].metrics.halstead.estimated_program_length
old: 44.039100017307746
new: 196.80702749979625
path: .spaces[2].metrics.halstead.effort
old: 732.5419866765973
new: 14779.3365234454
path: .spaces[2].metrics.halstead.n2
old: 6.0
new: 25.0
path: .spaces[2].metrics.halstead.volume
old: 97.67226489021296
new: 747.9421317533097
path: .spaces[2].metrics.halstead.difficulty
old: 7.5
new: 19.76
path: .spaces[2].metrics.halstead.time
old: 40.69677703758874
new: 821.0742513025223
path: .spaces[2].metrics.halstead.vocabulary
old: 15.0
new: 44.0
path: .spaces[2].metrics.halstead.length
old: 25.0
new: 137.0
path: .spaces[2].metrics.mi.mi_sei
old: 103.99863935991132
new: 42.09757947927643
path: .spaces[2].metrics.mi.mi_original
old: 124.48761963017122
new: 81.22819375788076
path: .spaces[2].metrics.mi.mi_visual_studio
old: 72.79977756150365
new: 47.50186769466711
path: .spaces[2].metrics.nargs.average
old: 2.0
new: 4.0
path: .spaces[2].metrics.nargs.sum
old: 2.0
new: 4.0
path: .spaces[2].metrics.cyclomatic.average
old: 1.0
new: 6.0
path: .spaces[2].metrics.cyclomatic.sum
old: 1.0
new: 6.0
path: .spaces[2].metrics.loc.ploc
old: 4.0
new: 26.0
path: .spaces[2].metrics.loc.blank
old: 0.0
new: 2.0
path: .spaces[2].metrics.loc.lloc
old: 1.0
new: 13.0
path: .spaces[2].metrics.loc.sloc
old: 4.0
new: 28.0
path: .spaces[2].metrics.cognitive.sum
old: 0.0
new: 8.0
path: .spaces[2].metrics.cognitive.average
old: 0.0
new: 8.0

### Code

    void nsPrimitiveHelpers::CreateDataFromPrimitive(const nsACString& aFlavor,
                                                     nsISupports* aPrimitive,
                                                     void** aDataBuff,
                                                     uint32_t* aDataLen) {
      if (!aDataBuff) return;

      *aDataBuff = nullptr;
      *aDataLen = 0;

      if (aFlavor.EqualsLiteral(kTextMime) ||
          aFlavor.EqualsLiteral(kCustomTypesMime)) {
        nsCOMPtr plainText(do_QueryInterface(aPrimitive));
        if (plainText) {
          nsAutoCString data;
          plainText->GetData(data);
          *aDataBuff = ToNewCString(data);
          *aDataLen = data.Length() * sizeof(char);
        }
      } else {
        nsCOMPtr doubleByteText(do_QueryInterface(aPrimitive));
        if (doubleByteText) {
          nsAutoString data;
          doubleByteText->GetData(data);
          *aDataBuff = ToNewUnicode(data);
          *aDataLen = data.Length() * sizeof(char16_t);
        }
      }
    }

## Minimal test - lines (40, 79)

path: .spaces[0].metrics.cyclomatic.average
old: 1.1666666666666667
new: 10.0
path: .spaces[0].metrics.cyclomatic.sum
old: 35.0
new: 10.0
path: .spaces[0].metrics.loc.cloc
old: 35.0
new: 2.0
path: .spaces[0].metrics.loc.ploc
old: 125.0
new: 35.0
path: .spaces[0].metrics.loc.sloc
old: 198.0
new: 40.0
path: .spaces[0].metrics.loc.lloc
old: 44.0
new: 15.0
path: .spaces[0].metrics.loc.blank
old: 38.0
new: 3.0
path: .spaces[0].metrics.mi.mi_sei
old: 7.7726886152048
new: 45.70037693723736
path: .spaces[0].metrics.mi.mi_original
old: 34.37819862399677
new: 71.67617338236444
path: .spaces[0].metrics.mi.mi_visual_studio
old: 20.104209721635534
new: 41.91589086687979
path: .spaces[0].metrics.nexits.average
old: 0.6785714285714286
new: 2.0
path: .spaces[0].metrics.nexits.sum
old: 19.0
new: 2.0
path: .spaces[0].metrics.cognitive.average
old: 0.25
new: 16.0
path: .spaces[0].metrics.cognitive.sum
old: 7.0
new: 16.0
path: .spaces[0].metrics.halstead.effort
old: 283386.37213614973
new: 37833.88932086374
path: .spaces[0].metrics.halstead.volume
old: 3829.006576364584
new: 1294.8496879867805
path: .spaces[0].metrics.halstead.length
old: 611.0
new: 225.0
path: .spaces[0].metrics.halstead.difficulty
old: 74.01041666666667
new: 29.21875
path: .spaces[0].metrics.halstead.N1
old: 366.0
new: 140.0
path: .spaces[0].metrics.halstead.n2
old: 48.0
new: 32.0
path: .spaces[0].metrics.halstead.purity_ratio
old: 0.6693283942607449
new: 1.1471444249334246
path: .spaces[0].metrics.halstead.estimated_program_length
old: 408.9596488933151
new: 258.1074956100205
path: .spaces[0].metrics.halstead.bugs
old: 1.4381296920031024
new: 0.37566384962138927
path: .spaces[0].metrics.halstead.n1
old: 29.0
new: 22.0
path: .spaces[0].metrics.halstead.N2
old: 245.0
new: 85.0
path: .spaces[0].metrics.halstead.time
old: 15743.687340897208
new: 2101.8827400479854
path: .spaces[0].metrics.halstead.level
old: 0.01351161154116819
new: 0.034224598930481284
path: .spaces[0].metrics.halstead.vocabulary
old: 77.0
new: 54.0
path: .spaces[0].metrics.nargs.average
old: 0.42857142857142855
new: 4.0
path: .spaces[0].metrics.nargs.sum
old: 12.0
new: 4.0
path: .spaces[0].metrics.nom.functions
old: 28.0
new: 1.0
path: .spaces[0].metrics.nom.total
old: 28.0
new: 1.0

### Code

    void nsPrimitiveHelpers ::CreatePrimitiveForData(const nsACString& aFlavor,
                                                     const void* aDataBuff,
                                                     uint32_t aDataLen,
                                                     nsISupports** aPrimitive) {
      if (!aPrimitive) return;

      if (aFlavor.EqualsLiteral(kTextMime) ||
          aFlavor.EqualsLiteral(kNativeHTMLMime) ||
          aFlavor.EqualsLiteral(kRTFMime) ||
          aFlavor.EqualsLiteral(kCustomTypesMime)) {
        nsCOMPtr primitive =
            do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
        if (primitive) {
          const char* start = reinterpret_cast(aDataBuff);
          primitive->SetData(Substring(start, start + aDataLen));
          NS_ADDREF(*aPrimitive = primitive);
        }
      } else {
        nsCOMPtr primitive =
            do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
        if (primitive) {
          if (aDataLen % 2) {
            auto buffer = mozilla::MakeUnique(aDataLen + 1);
            if (!MOZ_LIKELY(buffer)) return;

            memcpy(buffer.get(), aDataBuff, aDataLen);
            buffer[aDataLen] = 0;
            const char16_t* start = reinterpret_cast(buffer.get());
            // recall that length takes length as characters, not bytes
            primitive->SetData(Substring(start, start + (aDataLen + 1) / 2));
          } else {
            const char16_t* start = reinterpret_cast(aDataBuff);
            // recall that length takes length as characters, not bytes
            primitive->SetData(Substring(start, start + (aDataLen / 2)));
          }
          NS_ADDREF(*aPrimitive = primitive);
        }
      }

    }  // CreatePrimitiveForData

## Minimal test - lines (158, 197)

path: .spaces[3].metrics.mi.mi_original
old: 149.39820093838333
new: 71.6039682058588
path: .spaces[3].metrics.mi.mi_sei
old: 139.93701147893023
new: 49.508006430824565
path: .spaces[3].metrics.mi.mi_visual_studio
old: 87.3673689698148
new: 41.87366561746129
path: .spaces[3].metrics.nexits.average
old: 1.0
new: 2.0
path: .spaces[3].metrics.nexits.sum
old: 1.0
new: 2.0
path: .spaces[3].metrics.loc.cloc
old: 0.0
new: 3.0
path: .spaces[3].metrics.loc.ploc
old: 1.0
new: 35.0
path: .spaces[3].metrics.loc.lloc
old: 1.0
new: 17.0
path: .spaces[3].metrics.loc.sloc
old: 1.0
new: 40.0
path: .spaces[3].metrics.loc.blank
old: 0.0
new: 2.0
path: .spaces[3].metrics.cyclomatic.average
old: 1.0
new: 13.0
path: .spaces[3].metrics.cyclomatic.sum
old: 1.0
new: 13.0
path: .spaces[3].metrics.nargs.sum
old: 2.0
new: 3.0
path: .spaces[3].metrics.nargs.average
old: 2.0
new: 3.0
path: .spaces[3].metrics.cognitive.sum
old: 0.0
new: 18.0
path: .spaces[3].metrics.cognitive.average
old: 0.0
new: 18.0
path: .spaces[3].metrics.halstead.N1
old: 11.0
new: 136.0
path: .spaces[3].metrics.halstead.difficulty
old: 9.0
new: 28.673076923076923
path: .spaces[3].metrics.halstead.N2
old: 6.0
new: 71.0
path: .spaces[3].metrics.halstead.bugs
old: 0.02233552220035716
new: 0.3427228975522932
path: .spaces[3].metrics.halstead.effort
old: 548.4992626103368
new: 32968.30075798521
path: .spaces[3].metrics.halstead.n1
old: 9.0
new: 21.0
path: .spaces[3].metrics.halstead.length
old: 17.0
new: 207.0
path: .spaces[3].metrics.halstead.level
old: 0.1111111111111111
new: 0.034875922199865864
path: .spaces[3].metrics.halstead.volume
old: 60.94436251225965
new: 1149.799892297271
path: .spaces[3].metrics.halstead.n2
old: 3.0
new: 26.0
path: .spaces[3].metrics.halstead.time
old: 30.472181256129826
new: 1831.572264332512
path: .spaces[3].metrics.halstead.purity_ratio
old: 1.957894853832016
new: 1.0359908142513157
path: .spaces[3].metrics.halstead.estimated_program_length
old: 33.284212515144276
new: 214.45009855002235
path: .spaces[3].metrics.halstead.vocabulary
old: 12.0
new: 47.0

### Code

    nsresult nsLinebreakHelpers ::ConvertPlatformToDOMLinebreaks(
        const nsACString& inFlavor, void** ioData, int32_t* ioLengthInBytes) {
      NS_ASSERTION(ioData && *ioData && ioLengthInBytes, "Bad Params");
      if (!(ioData && *ioData && ioLengthInBytes)) return NS_ERROR_INVALID_ARG;

      nsresult retVal = NS_OK;

      if (inFlavor.EqualsLiteral(kTextMime) || inFlavor.EqualsLiteral(kRTFMime)) {
        char* buffAsChars = reinterpret_cast(*ioData);
        char* oldBuffer = buffAsChars;
        retVal = nsLinebreakConverter::ConvertLineBreaksInSitu(
            &buffAsChars, nsLinebreakConverter::eLinebreakAny,
            nsLinebreakConverter::eLinebreakContent, *ioLengthInBytes,
            ioLengthInBytes);
        if (NS_SUCCEEDED(retVal)) {
          if (buffAsChars != oldBuffer)  // check if buffer was reallocated
            free(oldBuffer);
          *ioData = buffAsChars;
        }
      } else if (inFlavor.EqualsLiteral("image/jpeg")) {
        // I'd assume we don't want to do anything for binary data....
      } else {
        char16_t* buffAsUnichar = reinterpret_cast(*ioData);
        char16_t* oldBuffer = buffAsUnichar;
        int32_t newLengthInChars;
        retVal = nsLinebreakConverter::ConvertUnicharLineBreaksInSitu(
            &buffAsUnichar, nsLinebreakConverter::eLinebreakAny,
            nsLinebreakConverter::eLinebreakContent,
            *ioLengthInBytes / sizeof(char16_t), &newLengthInChars);
        if (NS_SUCCEEDED(retVal)) {
          if (buffAsUnichar != oldBuffer)  // check if buffer was reallocated
            free(oldBuffer);
          *ioData = buffAsUnichar;
          *ioLengthInBytes = newLengthInChars * sizeof(char16_t);
        }
      }

      return retVal;

    }  // ConvertPlatformToDOMLinebreaks

## Minimal test - lines (86, 110)

path: .spaces[1].metrics.cognitive.average
old: 1.0
new: 2.0
path: .spaces[1].metrics.cognitive.sum
old: 1.0
new: 2.0
path: .spaces[1].metrics.nexits.average
old: 1.0
new: 2.0
path: .spaces[1].metrics.nexits.sum
old: 1.0
new: 2.0
path: .spaces[1].metrics.nargs.sum
old: 2.0
new: 3.0
path: .spaces[1].metrics.nargs.average
old: 2.0
new: 3.0
path: .spaces[1].metrics.mi.mi_sei
old: 136.79252132641966
new: 67.03245818349257
path: .spaces[1].metrics.mi.mi_original
old: 118.3691255569641
new: 84.01868032132573
path: .spaces[1].metrics.mi.mi_visual_studio
old: 69.2217108520258
new: 49.13373118206184
path: .spaces[1].metrics.halstead.volume
old: 151.23612512626255
new: 710.8406828659748
path: .spaces[1].metrics.halstead.purity_ratio
old: 1.428939605493969
new: 1.4633458089536195
path: .spaces[1].metrics.halstead.estimated_program_length
old: 52.87076540327685
new: 191.69830097292416
path: .spaces[1].metrics.halstead.difficulty
old: 10.0
new: 15.038461538461538
path: .spaces[1].metrics.halstead.time
old: 84.0200695145903
new: 593.8861260696499
path: .spaces[1].metrics.halstead.effort
old: 1512.3612512626255
new: 10689.9502692537
path: .spaces[1].metrics.halstead.N2
old: 14.0
new: 46.0
path: .spaces[1].metrics.halstead.length
old: 37.0
new: 131.0
path: .spaces[1].metrics.halstead.n2
old: 7.0
new: 26.0
path: .spaces[1].metrics.halstead.n1
old: 10.0
new: 17.0
path: .spaces[1].metrics.halstead.level
old: 0.1
new: 0.06649616368286446
path: .spaces[1].metrics.halstead.bugs
old: 0.04391866257410552
new: 0.1617567956757922
path: .spaces[1].metrics.halstead.N1
old: 23.0
new: 85.0
path: .spaces[1].metrics.halstead.vocabulary
old: 17.0
new: 43.0
path: .spaces[1].metrics.loc.ploc
old: 5.0
new: 20.0
path: .spaces[1].metrics.loc.blank
old: 0.0
new: 3.0
path: .spaces[1].metrics.loc.lloc
old: 1.0
new: 11.0
path: .spaces[1].metrics.loc.sloc
old: 5.0
new: 25.0
path: .spaces[1].metrics.cyclomatic.sum
old: 2.0
new: 3.0
path: .spaces[1].metrics.cyclomatic.average
old: 2.0
new: 3.0

### Code

    void nsPrimitiveHelpers ::CreatePrimitiveForCFHTML(const void* aDataBuff,
                                                       uint32_t* aDataLen,
                                                       nsISupports** aPrimitive) {
      if (!aPrimitive) return;

      nsCOMPtr primitive =
          do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
      if (!primitive) return;

      // We need to duplicate the input buffer, since the removal of linebreaks
      // might reallocte it.
      void* utf8 = moz_xmalloc(*aDataLen);
      memcpy(utf8, aDataBuff, *aDataLen);
      int32_t signedLen = static_cast(*aDataLen);
      nsLinebreakHelpers::ConvertPlatformToDOMLinebreaks(
          nsDependentCString(kTextMime), &utf8, &signedLen);
      *aDataLen = signedLen;

      nsAutoString str(
          NS_ConvertUTF8toUTF16(reinterpret_cast(utf8), *aDataLen));
      free(utf8);
      *aDataLen = str.Length() * sizeof(char16_t);
      primitive->SetData(str);
      NS_ADDREF(*aPrimitive = primitive);
    }